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 }'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: