1984 – the techblog

(Hopefully) useful various sysadmin and other stuff.

Get the sum of a column in shell (using awk) or: find out cumulative memory consumption

The title already gives it already away, awk is the tool to use here:

awk '{ SUM += $YOUR_FIELDNUMBER} END { print SUM }'

where YOUR_FIELDNUMBER is the num­ber of the col­umn you want to sum (the dol­lar sign is required!). I needed lit­tle one­liner to find out how much mem­ory php was con­sum­ing on a web­server. Since there were mul­ti­ple sites and processes (fast_cgi in effect) used, a bit more work was needed. The whole com­mand is:

ps -aux | grep php | awk '{ SUM += $6} END { print SUM/1024/1024 }'

This will give you res­i­dent mem­ory in giga­bytes. Vir­tual mem­ory would be $5 – see your system’s ps man page, it may vary.

Leave a Reply