Change output if file is empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change output if file is empty
# 1  
Old 03-24-2009
Change output if file is empty

I'm very new to writing scripts, so here is my problem...I have the following code already written (in perl)

system "rm file2";
open(FILE2, ">file2");
open(MYINPUTFILE, "file");

while(<MYINPUTFILE>) {
my($line) = $_;
chomp($line);
print file2 "$line\n";
print file2 "just a line\n";
print file2 " \n";
}

what I want it to do also is that if the input file is empty, write "There is nothing to do" instead of what it is printing now, but still print it into the same file. Any ideas?
# 2  
Old 03-24-2009
to check that the file exists and is of zero size:
Code:
if (-e $filename  and  -z $filename) {
    print "file $filename exists and is of zero size\n";
}

# 3  
Old 03-24-2009
ok, so I modified the code to what I have below. The problem is that it no always prints "message here", even if the file is not empty.

Code:
 
system "rm file2";
open(FILE2, ">file2");
 
open(MYINPUTFILE, "file");
 
if ( ! -s file ){
 print FILE2 "message here\n";
 } else {
 while(<MYINPUTFILE>) {
  my($line) = $_;
  chomp($line);
  print FILE2 "$line\n";
  print FILE2 "just a line\n";
  print FILE2 " \n";
 }
}

Any ideas?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Change the naming convention of the output file

Hi Currently we have nmon running on our Red hat Linux server. The ouput file is now coming with the naming convention as "servername_160321_0010.nmon". The output file naming convention has to be changed as "nmon_servername_daily_2016.03.21_00.00.00" How can we do it ? Any suggestions... (10 Replies)
Discussion started by: newtoaixos
10 Replies

3. Shell Programming and Scripting

How do I change a variable to something only if it's empty?

I feel like it is just a matter of using the $ operators correctly, but I can't seem to get it... hostname="network" ip="192.168.1.1" netmask="" variables=( $hostname $ip $netmask ) for var in ${variables} do if ; then $var="--" fi done echo... (7 Replies)
Discussion started by: etranman1
7 Replies

4. Shell Programming and Scripting

Change the font of text in output file in shell scipt

hi, I want to change the font of text in output file. :( I tried the below code code: if awk 'BEGIN{if('$RSS'>='1000')exit 0;exit 1}' then RED=`echo "\033 i can see colors in terminal but not in output file :wall: please help me how i can get colors text in output file. edit... (1 Reply)
Discussion started by: sreelu
1 Replies

5. Shell Programming and Scripting

Transfer output to empty file?

Hi all, I want transfer the echo data into file.txt.how? echo " $dir $group " >> ${file.txt} ---------- Post updated at 04:11 PM ---------- Previous update was at 03:10 PM ---------- anybody can help ? i mean in script output like echo " hello" i want transfer that output to file.txt. (4 Replies)
Discussion started by: proghack
4 Replies

6. Shell Programming and Scripting

Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done.. But I could not even do following simple task. How do I Change existing variable value only when user enteres non-empty string. ? #!/usr/bin/ksh uid="scott" # Assign new value user enters to uid, else leave it... (7 Replies)
Discussion started by: kchinnam
7 Replies

7. Shell Programming and Scripting

Change file output format

I have a file which has following contents usmtnz-dinfsi19 62 61 18400 18800 99.7 usmtnz-dinfsi19 62 61 18400 18800 99.7 i want the o/p to be like date (7 Replies)
Discussion started by: fugitive
7 Replies

8. Emergency UNIX and Linux Support

Read file and change a 0 to a 1 in output

<key>ExcludeSimpleHostnames</key> <integer>0</integer> <key>FTPPassive</key> Need simple command that will change the 0 to a 1 in this file when I grep it, but only for this integer key directly after the ExcludeSimpleHostnames key. I got this output code... (8 Replies)
Discussion started by: glev2005
8 Replies

9. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

10. UNIX for Dummies Questions & Answers

The output file is empty. Please Help

Dear all, Currently I writing a ksh script to perform some sql query. I already pipe results in a output file. But when I checked it, the output file is empty. Below is part of the script that I wrote: ------------------------------------------------------------------------ function... (4 Replies)
Discussion started by: balzzz
4 Replies
Login or Register to Ask a Question