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

I believe your problem is this line:

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:

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.

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

Hi gus2000,

Thanks! It's working now! You're great! :slight_smile:

Hi jyoung,

Thanks also for taking time to help me. :slight_smile: