Summing a column of numbers using awk

When I googled “awk sum count of column”  I found this answer from a site i’ve never heard about!  haha  http://www.commandlinefu.com/commands/view/1497/using-awk-to-sumcount-a-column-of-numbers.  It looks like a stack overflow for shell commands!

Inexperienced with awk, my first attempt was to extract the third column which had the count I wanted to sum, and find a way to add that.   Through some searching I found the program bc,  which is basically a command line calculator.

cat thumbnail-counts.txt  | awk '{ print $3 }' | paste -sd+ - | bc

 

However,  it turns out awk has a lot more power than just extracting columns of text  😀   And a succinct solution turns out to be this:

     cat count.txt | awk '{ sum+=$1} END {print sum}'

 

Leave a Reply

Your email address will not be published. Required fields are marked *