i wrote an awk progarm to calculate throughput from a ns2 trace file. i want this program to act on multiple trace files and it should display each output in a single output file can anyone please clear my doubt i tried with
awk -f awkscript inputfile1 inputfile2>outputfile
but it calculates the sum of all the throughputs and displaying
for file in inputfile[1-2]
do
awk -f awkscript "$file" >> outputfile
done
Or here is another example:
awk ' BEGIN {
F = FILENAME
} F == FILENAME {
t += $0
} F != FILENAME {
print t >> "outputfile"
t = 0
t += $0
} {
F = FILENAME
} END {
print t >> "outputfile"
} ' inputfile1 inputfile2
@bipinajith: In the first example instead of >> file you could use done > file instead, then you do not need to append...
regarding the awk, FILENAME is not initialized in the BEGIN section and also there you do not need to append from within awk, but you can redirect stdout to a file.. You could also let FNR==1 mark the beginning of a new file.
Beside gawk, mawk and bwk, it also does not work with /usr/xpg4/bin/awk. So it may work on HPUX, but it should not be relied upon.. According to the POSIX specification: