Question

I am trying to make a simple one liner i bash that outputs all programs running and their memory footprint. I have a basic solution witch looks like the following:

ps -ely | awk '{ print $8 "\t" $13 }' | sort -h

This will print out something like:

8348    urxvt
8376    urxvt
19716   X
320820  firefox

Now this works, but it would be nicer to have the memory output converted to a more human readable form, eg '320M firefox' instead of '320820 firefox'. I have tried looking at substr() in awk, but I cannot seem to get the syntax right. Does anyone have a good suggestion?

Was it helpful?

Solution

You can try this awk,

ps -ely | awk '{ if( $8 > 1024 )print int($8/1024) "M\t" $13 ; else print $8 "K\t" $13; }' | sort -h

OTHER TIPS

Maybe this can do it for you

ps -ely | awk '{ print $8/1024/1024 "\t" $13 }'

The output would something like :

0.082 ps
0.089 awk
0.053 oracle
0.046 mingetty

With sed:

sed -r 's/^([0-9]{3})[0-9]*/\1/g'

Substitute all numbers by the 3 last.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top