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
451 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 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

778 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