I have a file that contains data in the following format:
Field 1 Field 2 nCalls duration
My task is to sum the nCalls and duration fields. The code I have written so far is as follows:
cat ${file} |\
while read n1 n2 nCalls nDuration
do
#sumCalls=$((${sumCalls} + ${nCalls}))
#sumDuration=$((${sumDuration} + ${nDuration}))
sumCalls=`expr ${sumCalls} + ${nCalls}`
sumDuration=`expr ${sumDuration} + ${nDuration}`
echo "${sumCalls} ${sumDuration}"
echo -n "${appName} ${sumCalls} ${sumDuration}" > temp.txt
done
tail -n1 temp.txt >> ${outFile}
Both the statements above do not work. I get syntax errors.
My question is:
1. Where am I going wrong with the above code?
2. Is there a better way to do this than write into and out of files?
Thanks,
Sriram
This is what
awkwas built for:The following transcript shows it in action: