Visit Our UNIX and Linux User Community


Append output to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append output to file
# 1  
Old 10-24-2007
Append output to file

Hi,

I have a script below. It get's the data from the output of a script that is running hourly. My problem is every time my script runs, it deletes the previous data and put the current data. Please see output below. What I would like to do is to have the hourly output to be appended on the file like the expected output below. Please help me how to modify my script to do it. Thanks!


Output:
DateTime succDeliv failDeliv Subs SysFail Tot
2007-10-24_15H 12 1 43 0 1856

Expected output:
DateTime succDeliv failDeliv Subs SysFail Tot
2007-10-24_00H 12 13 43 6 1856
2007-10-24_01H 16 21 80 0 1780
2007-10-24_03H 0 8 56 9 1867
2007-10-24_04H 98 0 67 0 1909

Script:
#!/bin/ksh -f

######get date today############
ngayon=`TZ=GMT+1 date '+%Y-%m-%d'`
dateF=`TZ=GMT date '+%Y-%m-%d_%HH'`
outFayl="RSC-FinalStat-${ngayon}.txt"
workDir="/export/Stats"


printf "%-20s %-20s %-20s %-20s %-20s %-20s\n" DateTime succDeliv failDeliv Subs SysFail Tot > $workDir/$outFayl


cd $workDir
DateTime=dateF
succDeliv=`sed -n 2p $workDir/Stats_$dateF.txt`
failDeliv=`sed -n 4p $workDir/Stats_$dateF.txt`
AbsSubs=`sed -n 6p $workDir/Stats_$dateF.txt`
SysFail=`sed -n 8p $workDir/Stats_$dateF.txt`
Tot=`sed -n 10p $workDir/Stats_$dateF.txt`


printf "%-20s %-20s %-20s %-20s %-20s %-20s\n" $dateF $succDeliv $failDeliv $Subs $SysFail $Tot >> $workDir/$outFayl
# 2  
Old 10-24-2007
I believe your problem is this line:
Code:
printf "%-20s %-20s %-20s %-20s %-20s %-20s\n" DateTime succDeliv failDeliv Subs SysFail Tot > $workDir/$outFayl

You are over writing it everytime you execute the script. You would have to change it to this:
Code:
printf "%-20s %-20s %-20s %-20s %-20s %-20s\n" DateTime succDeliv failDeliv Subs SysFail Tot >> $workDir/$outFayl

Of course you will get your headings multiple times. Not sure how to solve that.
# 3  
Old 10-24-2007
I believe you want the first "printf" to only write the header if the file does not exist:
if [[ ! -f $workDir/$outFayl ]]; then
printf...
fi
# 4  
Old 10-24-2007
Append output to file

Hi gus2000,

Thanks! It's working now! You're great! Smilie

Hi jyoung,

Thanks also for taking time to help me. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #424
Difficulty: Medium
C# was designed by Anders Hejlsberg.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

Need solution to compare two file and update and append the output

Hi All, I have two files File1 frame,007C1 server1_Parent frame,007C3 server2_Silver frame,007EE server3_Bronze frame,00855 server4_Parent frame,00856 server4_Parent frame,00858 server5_Parent frame,008FA server6_Silver frame,008FB server6_Silver frame,008FC server6_Silver... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

3. Shell Programming and Scripting

Append to a file repeating output

Hello, i'm trying to force a command to read every second from an interface watch -n1 (command) /dev/x | cat >> output but it continue to overwrite the file, without append the content Thanks and advace for help as usual regards (4 Replies)
Discussion started by: Board27
4 Replies

4. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

5. UNIX for Dummies Questions & Answers

Noob questions.. Append output to a file in different directory

Noob question! I know almost nothing so far, and I'm trying to teach myself from books, on a typical command line without using scripts how would I append output from a sort to a file in a completely different directory? example: If I'm sorting a file in my documents directory but I... (2 Replies)
Discussion started by: Byrang
2 Replies

6. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

7. Shell Programming and Scripting

append an output file with two columns

Hi All, can you help me with this: grep XXX dir/*.txt|wc -l > newfile.txt - this put the results in the newfile.txt, but I want to add another column in the newfile.txt, string 'YYYYY', separated somehow, which corresponds on the grep results? For example grep will grep XXX dir/*.txt|wc -l >... (5 Replies)
Discussion started by: apenkov
5 Replies

8. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

9. UNIX for Dummies Questions & Answers

Output to file but append rather than overwrite?

I am running a command which has a parameter that outputs the results to a file each time it is run. Here is the command: --fullresult=true > importlog.xml Can I add the output to the file rather than creating a new one which overwrites the existing one? If not can I make the file name... (2 Replies)
Discussion started by: Sepia
2 Replies

10. Shell Programming and Scripting

Append Output to another file in Perl

Hi All, I am writing a Perl script such that the output from "perl myscript.pl file1" to be appended to another file name called file2. I tried out with the below code but couldn't work. Can any expert give me some advice? open(OUTPUT, 'perl myscript.pl file1 |'); close OUTPUT;... (7 Replies)
Discussion started by: Raynon
7 Replies

Featured Tech Videos