Friday, May 11, 2007

Adding Using awk

Today I had a need to take a file that was formatted like:

40.00
34.00
100.50
...
3902.00
50.00

and add up all the numbers. The file was huge so I didn't feel like opening it with Open Office. I knew there had to be an easy way to do this without leaving my shell in Linux. So after about 5 seconds of Googling I found this little gem:

awk '{x+=$1} END{print x}'

Works like a champ:

13300.8