Solved

If /path/file1.txt has 0 data replace with /path/genericfile.txt using shell script or perl on a linux box

Posted on 2011-02-21
4
453 Views
Last Modified: 2012-05-11
I'm running a matlab script at it will not create ANY graphics unless ALL of the files contain data. As a result, I need to replace files that occasionally lack to with dummy data so the remaining output graphics can be generated so I need to do this.

If /path/file1.txt has 0 data replace with /path/genericfile.txt using shell script or perl on a linux box
0
Comment
Question by:libertyforall2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Pieter Jordaan earned 250 total points
ID: 34949231
Hi

There are several ways to do that.

The simplest way is to cat the file, and then count the amount of lines.
If it has less than 1 line, then replace it with the contents of the other file.


LINES=`cat /path/file1.txt | wc -l`
if [ "$LINES" -lt 1 ]
   then
   cat /path/genericfile.txt > /path/file1.txt
   fi

Open in new window

0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 34949339
Yes, there are several ways.

[ -s /path/file1.txt ] && cp -p /path/file1.txt /path/genericfile.txt

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34949349
Sorry of course it's the other way!

[ -s /path/file1.txt ] && cp -p /path/genericfile.txt /path/file1.txt

wmp
0
 

Author Closing Comment

by:libertyforall2
ID: 34975194
Works.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question