Adding a column of numbers on the Unix command line

Here’s a simple tip to add up a column of numbers from the UNIX command line using the awk command. This can be especially useful when used at the end of a pipeline:

# Basic Usage (where <col> is the column number)
awk '{ sum+=$<col>> } END { print sum}'

# Adding up the size of all the files in the current directory, you could use the following:
ls -l | awk '{ sum+=$5 } END { print sum}'

# Adding up the CPU being used for a particular oracle instance:
ps -eo %cpu,cmd | grep oracleprod | awk '{ sum+=$1 } END { print sum }'