Unix question (scripting)
Posted in 2011
A user on Tru64 wanted to sort `ps -eo pid,pmem,pcpu,rss,vsize,args` output by RSS, but the RSS column mixes K/M/G suffixes so a plain numeric sort fails. Replies agreed the answer is post-processing: strip the suffix and multiply to a common unit (awk/perl or shell) before sorting. Jacques Lapeire posted a working ksh loop using a `case` on the K/M/G suffix with sed to strip the letter, multiplying by 1024 (M) or 1024*1024 (G), then piping to `sort -nr`. The original poster adapted it to run the ps command directly and sort on the computed column, confirming it worked.
Auto-generated by DrWatson from the posts below — may be imperfect; read the full thread.
Topics: General Discussion
Another unix question (Tru64) - but the solution should probably work on all Unix flavours ? When I run: ps -eo pid,pmem,pcpu,rss,vsize,args PID %MEM %CPU RSS VSZ COMMAND 0 1.7 35.0 1.1G 70.8G [kernel idle] 1 0.0 0.0 88K 624K /sbin/init -a 3 0.0 0.0 960K 1.45M /sbin/kloadsrv 5 0.0 0.0 928K 4.61M /sbin/hotswapd 58 0.0 0.0 328K 2.95M /usr/sbin/esmd 69 0.0 0.0 112K 2.18M /sbin/update 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info -l /var/evm/adm/logfiles/evmlogger.log 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l /var/evm/adm/logfiles/evmchmgr.log 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd 470 0.0 0.0 168K 2.81M /usr/sbin/portmap The RSS column is not numeric. It contains Kb, Mb and Gb. How can I sort this column in a script, so I get highest to lowest RSS ? Dirk NOTE: This e-mail message is subject to the MTN Group disclaimer see http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx
Hello Dirk, there are probably better solutions, but you could try something like this : The input file used was your output below. Result : PID %MEM %CPU RSS VSZ COMMAND 1048576 0 1.7 35.0 1.1G 70.8G [kernel idle] 3072 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd 2048 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd 960 3 0.0 0.0 960K 1.45M /sbin/kloadsrv 928 5 0.0 0.0 928K 4.61M /sbin/hotswapd 440 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info -l /var/evm/adm/logfiles/evmlogger.log 360 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l /var/evm/adm/logfiles/evmchmgr.log 344 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e 328 58 0.0 0.0 328K 2.95M /usr/sbin/esmd 168 470 0.0 0.0 168K 2.81M /usr/sbin/portmap 112 69 0.0 0.0 112K 2.18M /sbin/update 88 1 0.0 0.0 88K 624K /sbin/init -a Script used (Korn-shell) : #!/usr/bin/ksh head -n 1 input cat input | egrep -v '%MEM|^$' | \\\\ while read PID MEM CPU RSS dummy do case $RSS in *M) CALC=`echo $RSS | sed -e 's/M//'` ; ((CALC = $CALC * 1024)) ;; *G) CALC=`echo $RSS | sed -e 's/G//'` ; ((CALC = $CALC * 1024 * 1024)) ;; *K) CALC=`echo $RSS | sed -e 's/K//'` ;; esac echo $CALC $PID $MEM $CPU $RSS $dummy done |sort -nr exit 0 Good luck, Jacques Lapeire -----Original Message----- From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of Dirk Cornel.... Sent: Thursday, August 25, 2011 11:28 AM To: ids@iiug.org Subject: Unix question (scripting) [24708] Another unix question (Tru64) - but the solution should probably work on all Unix flavours ? When I run: ps -eo pid,pmem,pcpu,rss,vsize,args PID %MEM %CPU RSS VSZ COMMAND 0 1.7 35.0 1.1G 70.8G [kernel idle] 1 0.0 0.0 88K 624K /sbin/init -a 3 0.0 0.0 960K 1.45M /sbin/kloadsrv 5 0.0 0.0 928K 4.61M /sbin/hotswapd 58 0.0 0.0 328K 2.95M /usr/sbin/esmd 69 0.0 0.0 112K 2.18M /sbin/update 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info -l /var/evm/adm/logfiles/evmlogger.log 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l /var/evm/adm/logfiles/evmchmgr.log 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd 470 0.0 0.0 168K 2.81M /usr/sbin/portmap The RSS column is not numeric. It contains Kb, Mb and Gb. How can I sort this column in a script, so I get highest to lowest RSS ? Dirk NOTE: This e-mail message is subject to the MTN Group disclaimer see http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx ******************************************************************************* Forum Note: Use "Reply" to post a response in the discussion forum.
Post process. Do up a little script to change the K/M/G to numbers and = multiply, then sort. Of course that is failing some simple command line option of which I am = obliviously unaware. j. On Aug 25, 2011, at 5:28 AM, Dirk Cornel.... wrote: > Another unix question (Tru64) - but the solution should probably work = on all=20 > Unix flavours ?=20 >=20 > When I run:=20 >=20 > ps -eo pid,pmem,pcpu,rss,vsize,args=20 >=20 > PID %MEM %CPU RSS VSZ COMMAND=20 >=20 > 0 1.7 35.0 1.1G 70.8G [kernel idle]=20 >=20 > 1 0.0 0.0 88K 624K /sbin/init -a=20 >=20 > 3 0.0 0.0 960K 1.45M /sbin/kloadsrv=20 >=20 > 5 0.0 0.0 928K 4.61M /sbin/hotswapd=20 >=20 > 58 0.0 0.0 328K 2.95M /usr/sbin/esmd=20 >=20 > 69 0.0 0.0 112K 2.18M /sbin/update=20 >=20 > 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd=20 >=20 > 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info = -l=20 > /var/evm/adm/logfiles/evmlogger.log=20 >=20 > 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l=20 > /var/evm/adm/logfiles/evmchmgr.log=20 >=20 > 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e=20 >=20 > 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd=20 >=20 > 470 0.0 0.0 168K 2.81M /usr/sbin/portmap=20 >=20 > The RSS column is not numeric. It contains Kb, Mb and Gb. How can I = sort this=20 > column in a script, so I get highest to lowest RSS ?=20 >=20 > Dirk=20 >=20 > NOTE: This e-mail message is subject to the MTN Group disclaimer see=20= > http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx=20 >=20 >=20 > = **************************************************************************= *****=20 > Forum Note: Use "Reply" to post a response in the discussion forum.=20= >=20
Process the report through an awk or perl script that expands the K,M,G into the appropriate numbers on output then you can sort the resulting modified report. Art Art S. Kagel Advanced DataTools (www.advancedatatools.com) Blog: http://informix-myview.blogspot.com/ Disclaimer: Please keep in mind that my own opinions are my own opinions and do not reflect on my employer, Advanced DataTools, the IIUG, nor any other organization with which I am associated either explicitly, implicitly, or by inference. Neither do those opinions reflect those of other individuals affiliated with any entity with which I am affiliated nor those of the entities themselves. On Thu, Aug 25, 2011 at 5:28 AM, Dirk Cornel.... <moolma_dc@mtn.co.za>wrote: > Another unix question (Tru64) - but the solution should probably work on > all > Unix flavours ? > > When I run: > > ps -eo pid,pmem,pcpu,rss,vsize,args > > PID %MEM %CPU RSS VSZ COMMAND > > 0 1.7 35.0 1.1G 70.8G [kernel idle] > > 1 0.0 0.0 88K 624K /sbin/init -a > > 3 0.0 0.0 960K 1.45M /sbin/kloadsrv > > 5 0.0 0.0 928K 4.61M /sbin/hotswapd > > 58 0.0 0.0 328K 2.95M /usr/sbin/esmd > > 69 0.0 0.0 112K 2.18M /sbin/update > > 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd > > 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info -l > /var/evm/adm/logfiles/evmlogger.log > > 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l > /var/evm/adm/logfiles/evmchmgr.log > > 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e > > 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd > > 470 0.0 0.0 168K 2.81M /usr/sbin/portmap > > The RSS column is not numeric. It contains Kb, Mb and Gb. How can I sort > this > column in a script, so I get highest to lowest RSS ? > > Dirk > > NOTE: This e-mail message is subject to the MTN Group disclaimer see > http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx > > > > ******************************************************************************* > Forum Note: Use "Reply" to post a response in the discussion forum. > > --000e0cd23e72e72c7704ab52dab1
Great. The script does not make provision for Kb yet (I will add that next), but here is a working script, replacing "input" with the command I want to use: RSS [Tru64 UNIX] Real memory (resident set) size of the process (in 1024 byte units) ##input = "ps -eo pid,pmem,pcpu,rss,vsize,args" #!/usr/bin/ksh ps -eo pid,pmem,pcpu,rss,vsize,args | head -n 1 ps -eo pid,pmem,pcpu,rss,vsize,args | egrep -v '%MEM|^$' | \\\\ while read PID MEM CPU RSS VSIZE PROG do case $RSS in *M) CALC=`echo $RSS | sed -e 's/M//'` ; ((CALC = $CALC * 1024)) ;; *G) CALC=`echo $RSS | sed -e 's/G//'` ; ((CALC = $CALC * 1024 * 1024)) ;; *K) CALC=`echo $RSS | sed -e 's/K//'` ;; esac echo $PID $MEM $CPU $CALC $VSIZE $PROG #done done |sort -n -k 4 exit 0 Thanks for your help Jacques :-) > -----Original Message----- > From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of > Lapeire, Jacques > Sent: Thursday, 25 August 2011 01:22 PM > To: ids@iiug.org > Subject: RE: Unix question (scripting) [24709] > > Hello Dirk, > > there are probably better solutions, but you could try something like > this : > > The input file used was your output below. > > Result : > > PID %MEM %CPU RSS VSZ COMMAND > 1048576 0 1.7 35.0 1.1G 70.8G [kernel idle] > 3072 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd > 2048 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd > 960 3 0.0 0.0 960K 1.45M /sbin/kloadsrv > 928 5 0.0 0.0 928K 4.61M /sbin/hotswapd > 440 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o > /var/run/evmlogger.info -l > /var/evm/adm/logfiles/evmlogger.log > 360 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l > /var/evm/adm/logfiles/evmchmgr.log > 344 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e > 328 58 0.0 0.0 328K 2.95M /usr/sbin/esmd > 168 470 0.0 0.0 168K 2.81M /usr/sbin/portmap > 112 69 0.0 0.0 112K 2.18M /sbin/update > 88 1 0.0 0.0 88K 624K /sbin/init -a > > Script used (Korn-shell) : > > #!/usr/bin/ksh > head -n 1 input > cat input | egrep -v '%MEM|^$' | \\\\ > while read PID MEM CPU RSS dummy > do > > case $RSS in > > *M) CALC=`echo $RSS | sed -e 's/M//'` ; > > ((CALC = $CALC * 1024)) ;; > > *G) CALC=`echo $RSS | sed -e 's/G//'` ; > > ((CALC = $CALC * 1024 * 1024)) ;; > > *K) CALC=`echo $RSS | sed -e 's/K//'` ;; > > esac > > echo $CALC $PID $MEM $CPU $RSS $dummy > done |sort -nr > exit 0 > > Good luck, > > Jacques Lapeire > -----Original Message----- > From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of > Dirk > Cornel.... > Sent: Thursday, August 25, 2011 11:28 AM > To: ids@iiug.org > Subject: Unix question (scripting) [24708] > > Another unix question (Tru64) - but the solution should probably work > on all > Unix flavours ? > > When I run: > > ps -eo pid,pmem,pcpu,rss,vsize,args > > PID %MEM %CPU RSS VSZ COMMAND > > 0 1.7 35.0 1.1G 70.8G [kernel idle] > > 1 0.0 0.0 88K 624K /sbin/init -a > > 3 0.0 0.0 960K 1.45M /sbin/kloadsrv > > 5 0.0 0.0 928K 4.61M /sbin/hotswapd > > 58 0.0 0.0 328K 2.95M /usr/sbin/esmd > > 69 0.0 0.0 112K 2.18M /sbin/update > > 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd > > 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info - > l > /var/evm/adm/logfiles/evmlogger.log > > 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l > /var/evm/adm/logfiles/evmchmgr.log > > 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e > > 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd > > 470 0.0 0.0 168K 2.81M /usr/sbin/portmap > > The RSS column is not numeric. It contains Kb, Mb and Gb. How can I > sort this > column in a script, so I get highest to lowest RSS ? > > Dirk > > NOTE: This e-mail message is subject to the MTN Group disclaimer see > http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx > > > *********************************************************************** > ******** > Forum Note: Use "Reply" to post a response in the discussion forum. > > > *********************************************************************** > ******** > Forum Note: Use "Reply" to post a response in the discussion forum. NOTE: This e-mail message is subject to the MTN Group disclaimer see http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx
Oh, sorry, my mistake - it already makes provision for Kb (last line in the CASE statement). > -----Original Message----- > From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of > Dirk Cornel.... > Sent: Tuesday, 30 August 2011 11:17 AM > To: ids@iiug.org > Subject: RE: Unix question (scripting) [24765] > > Great. The script does not make provision for Kb yet (I will add that > next), > but here is a working script, replacing "input" with the command I want > to > use: > > RSS [Tru64 UNIX] Real memory (resident set) > > size of the process (in 1024 byte units) > > ##input = "ps -eo pid,pmem,pcpu,rss,vsize,args" > > #!/usr/bin/ksh > ps -eo pid,pmem,pcpu,rss,vsize,args | head -n 1 > > ps -eo pid,pmem,pcpu,rss,vsize,args | egrep -v '%MEM|^$' | \\\\ > while read PID MEM CPU RSS VSIZE PROG > do > > case $RSS in > > *M) CALC=`echo $RSS | sed -e 's/M//'` ; > > ((CALC = $CALC * 1024)) ;; > > *G) CALC=`echo $RSS | sed -e 's/G//'` ; > > ((CALC = $CALC * 1024 * 1024)) ;; > > *K) CALC=`echo $RSS | sed -e 's/K//'` ;; > > esac > > echo $PID $MEM $CPU $CALC $VSIZE $PROG > #done > done |sort -n -k 4 > exit 0 > > Thanks for your help Jacques :-) > > -----Original Message----- > > From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of > > Lapeire, Jacques > > Sent: Thursday, 25 August 2011 01:22 PM > > To: ids@iiug.org > > Subject: RE: Unix question (scripting) [24709] > > > > Hello Dirk, > > > > there are probably better solutions, but you could try something like > > this : > > > > The input file used was your output below. > > > > Result : > > > > PID %MEM %CPU RSS VSZ COMMAND > > 1048576 0 1.7 35.0 1.1G 70.8G [kernel idle] > > 3072 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd > > 2048 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd > > 960 3 0.0 0.0 960K 1.45M /sbin/kloadsrv > > 928 5 0.0 0.0 928K 4.61M /sbin/hotswapd > > 440 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o > > /var/run/evmlogger.info -l > > /var/evm/adm/logfiles/evmlogger.log > > 360 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l > > /var/evm/adm/logfiles/evmchmgr.log > > 344 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e > > 328 58 0.0 0.0 328K 2.95M /usr/sbin/esmd > > 168 470 0.0 0.0 168K 2.81M /usr/sbin/portmap > > 112 69 0.0 0.0 112K 2.18M /sbin/update > > 88 1 0.0 0.0 88K 624K /sbin/init -a > > > > Script used (Korn-shell) : > > > > #!/usr/bin/ksh > > head -n 1 input > > cat input | egrep -v '%MEM|^$' | \\\\ > > while read PID MEM CPU RSS dummy > > do > > > > case $RSS in > > > > *M) CALC=`echo $RSS | sed -e 's/M//'` ; > > > > ((CALC = $CALC * 1024)) ;; > > > > *G) CALC=`echo $RSS | sed -e 's/G//'` ; > > > > ((CALC = $CALC * 1024 * 1024)) ;; > > > > *K) CALC=`echo $RSS | sed -e 's/K//'` ;; > > > > esac > > > > echo $CALC $PID $MEM $CPU $RSS $dummy > > done |sort -nr > > exit 0 > > > > Good luck, > > > > Jacques Lapeire > > > -----Original Message----- > > From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of > > Dirk > > Cornel.... > > Sent: Thursday, August 25, 2011 11:28 AM > > To: ids@iiug.org > > Subject: Unix question (scripting) [24708] > > > > Another unix question (Tru64) - but the solution should probably work > > on all > > Unix flavours ? > > > > When I run: > > > > ps -eo pid,pmem,pcpu,rss,vsize,args > > > > PID %MEM %CPU RSS VSZ COMMAND > > > > 0 1.7 35.0 1.1G 70.8G [kernel idle] > > > > 1 0.0 0.0 88K 624K /sbin/init -a > > > > 3 0.0 0.0 960K 1.45M /sbin/kloadsrv > > > > 5 0.0 0.0 928K 4.61M /sbin/hotswapd > > > > 58 0.0 0.0 328K 2.95M /usr/sbin/esmd > > > > 69 0.0 0.0 112K 2.18M /sbin/update > > > > 195 0.0 0.0 2.2M 5.52M /usr/sbin/evmd > > > > 214 0.0 0.0 440K 3.13M /usr/sbin/evmlogger -o /var/run/evmlogger.info > - > > l > > /var/evm/adm/logfiles/evmlogger.log > > > > 215 0.0 0.0 360K 2.88M /usr/sbin/evmchmgr -l > > /var/evm/adm/logfiles/evmchmgr.log > > > > 377 0.0 0.0 344K 3.08M /usr/sbin/syslogd -e > > > > 381 0.0 0.0 3.1M 5.80M /usr/sbin/binlogd > > > > 470 0.0 0.0 168K 2.81M /usr/sbin/portmap > > > > The RSS column is not numeric. It contains Kb, Mb and Gb. How can I > > sort this > > column in a script, so I get highest to lowest RSS ? > > > > Dirk > > > > NOTE: This e-mail message is subject to the MTN Group disclaimer see > > http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx > > > > > > > *********************************************************************** > > ******** > > Forum Note: Use "Reply" to post a response in the discussion forum. > > > > > > > *********************************************************************** > > ******** > > Forum Note: Use "Reply" to post a response in the discussion forum. > > NOTE: This e-mail message is subject to the MTN Group disclaimer see > http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx > > > *********************************************************************** > ******** > Forum Note: Use "Reply" to post a response in the discussion forum. NOTE: This e-mail message is subject to the MTN Group disclaimer see http://www.mtn.co.za/SUPPORT/LEGAL/Pages/EmailDisclaimer.aspx