Add to Favourites Add to Favourites    Print this Article Print this Article

Using awk to add up numbers

For those apt with unix commands, often times, you'll need to add up many numbers given to you in the shell.
The quick and easy way to do this, is to use awk.

For this example, we'll assume you've already got your list of numbers (one per line) in a file called numbers.txt.
The command would then be:

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

This method is handy when (for example), you're got the output when trying to find the number of occurence over many logs.. or perhaps many IPs in a log, etc..  

Was this answer helpful?

Also Read