Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
479 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
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Pieter Jordaan earned 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

972 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