• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

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

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
libertyforall2
Asked:
libertyforall2
  • 2
2 Solutions
 
Pieter JordaanSenior Systems Administrator - Web hosting and Network Security.Commented:
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
 
woolmilkporcCommented:
Yes, there are several ways.

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

wmp
0
 
woolmilkporcCommented:
Sorry of course it's the other way!

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

wmp
0
 
libertyforall2Author Commented:
Works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now