Aggregate processes based on name (~command) and sort in decreasing order of a "top" column

The top command has lots of useful columns, but since many applications run multiple worker processes (eg. webservers like Apache), it's a bit difficult to see the big picture. The following one-liner aggregates a selected column in the output of top by the process name (ie. the COMMAND column) and sorts the result in decreasing order:
top -b -n 1 | tail -n +8 | awk '{ col=$6; if (match(col, "^[0-9]+m$")) { col = 1000 * substr(col, 1, length(col) - 1) }; stat[$12] += col } END { for (i in stat) { print stat[i] ": " i } }' | sort -rn | head -n 20

It's logic heavily depends on the output format of top ... which might not be very strictly defined (ie. it might change). But the script is easily adjustable to various top formats. I've tested it on a Debian Squeeze server and it worked well. By default it calculates based on the RES (Resident size) column, but you can change col=$6 to col=$10 so it sorts based on MEM (a task's currently used share of available physical memory).