Solved

Linux: Only include items when value is more than 0

Posted on 2013-11-20
3
309 Views
Last Modified: 2013-11-20
I use the following code to identify the top 200 CPU-hogging processes and return results in JSON format:
# Thanks simon3270 and jb1dev
cat ps.txt | sort | awk '{if (s == $1){n=n+$2} else {if (s != ""){print s " " n};s=$1;n=$2}}END{print s " " n}' | sort -rn -k +2 | awk 'BEGIN{print "{\"hogs\":{"}NR <= 200{printf " \"%s\":%.1f",$1,$2; printf ","} END{print "}}"}'
#
# or
#
ps -eo "%c %C" | sort | awk '{if (s == $1){n=n+$2} else {if (s != ""){print s " " n};s=$1;n=$2}}END{print s " " n}' | sort -rn -k +2 | awk 'BEGIN{print "{\"hogs\":{"}NR <= 200{printf " \"%s\":%.1f",$1,$2; printf ","} END{print "}}"}' 

Open in new window

(See the attached file, ps.txt)

I want all processes with the value of 0 to be excluded.  So the results should only contain an item if the value is more than 0.  Also, the last item should not have a comma after it.
ps.txt
0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 39664477
You sample input contains the first line "COMMAND %CPU"

Do you want this line to be removed from the output?
0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39664492
cat ps.txt | grep -v '%CPU' | grep -v '0.0' | sort | awk '{if (s == $1){n=n+$2} else {if (s != ""){print s " " n};s=$1;n=$2}}END{print s " " n}' | sort -rn -k +2 | awk 'BEGIN{print "{\"hogs\":{"}NR <= 200{ if(NR != 1) { printf ", " } printf " \"%s\":%.1f",$1,$2; } END{print "}}"}'

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39664887
Thanks.  I have asked a related question here:
http://www.experts-exchange.com/OS/Linux/Q_28300077.html
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now