Wednesday, December 7, 2011

use awk command to sum up numbers in a column

data.txt:
price: 1
price: 3
price: 5
price: 7
price: 9

# cat data.txt | awk -F'\t' '{s = s +$2} {print s} END{print "sum: " s}'
1
4
9
16
25
sum: 25

-F'\t' : use tab as a separator

The Unix Programming Environment P.118

1 comment:

Akhil said...

Nice Article.
Some examples on Find Command and Awk Command