if file is NOT empty, then append content to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if file is NOT empty, then append content to file
# 1  
Old 11-08-2010
if file is NOT empty, then append content to file

hi people,

i have texts down.txt and down-new.txt and i want to check;

- if down-new.txt is NOT empty, then write date and its content to /home/gc_sw/down.txt

for example;

down.txt:
Code:
AAAA
SSSS

down-new.txt:
Code:
123
456

and after checking down-new.txt is NOT empty, down.txt should be:
Code:
AAAA
SSSS
 
20101108-1300
123
456

please write your codes in awk.

thx Smilie

Last edited by gc_sw; 11-08-2010 at 08:37 AM..
# 2  
Old 11-08-2010
Somethig like this,
Code:
awk '{print}' down-new.txt >> /home/gc_sw/down.txt

# 3  
Old 11-08-2010
Question Why the 'awk' requirement

Whenever someone specifies how a solution is to be done, it is often because this is not a real problem but rather a homework problem.
Please explain/clarify.
# 4  
Old 11-08-2010
try:
Code:
[ ! -s file ]

# 5  
Old 11-08-2010
pravin27:
thanks but where is the check of if file is not zero and date?

joeyg;
bacause my all code block is in awk, i prefer it rather than other ways. if there are alternatives you can surely express it Smilie

Scrutinizer;
thanks dude but can you please write the whole code? i have tried some combinations with if, !-s, NF, NR, etc.. but no results Smilie
# 6  
Old 11-08-2010
It was not awk but rather a standard shell test to see if a file is not empty:
Code:
if [ ! -s down-new.txt ]; then
  date >> down.txt
  cat down-new.txt >> down.txt
fi

In awk you can something like this:
Code:
awk 'END{if(!NR)print "empty"}' file

But I think it becomes more complicated..
# 7  
Old 11-08-2010
With awk you can do something like:
Code:
if(system("[ -s down-new.txt ]") == 0){
  # file is NOT empty
}

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

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

3. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

5. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

6. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

7. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

8. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 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

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies
Login or Register to Ask a Question