Using awk to add up numbers


Enter Your Query:
Use '%' for wildcards and quotes for "exact phrases"


Top Level » System Level

Using awk to add up numbersLast Modified: Jan 23, 2013, 3:59 pm
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..  

2003 JBMC Software, Suite 173  3-11 Bellerose Drive, St Albert, AB  T8N 1P7  Canada.  Mon-Fri 9AM-5PM MST