A Developer's Diary

Apr 9, 2009

Using awk to calculate sum/average

awk is a small powerful tool for processing column oriented text data.
Suppose I have file Marks.txt whose contents are




Use the following command to calculate the sum of the entries in column 3


cat Marks.txt | awk '{sum+=$3} END {print "Total=", sum}'



To get the average, use the following command

cat Marks.txt | awk '{sum+= $3} END {print "Average=", sum/NR}'


No comments :

Post a Comment