Solved

Linux: Only include items when value is more than 0

Posted on 2013-11-20
3
308 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.  I have asked a related question here:
http://www.experts-exchange.com/OS/Linux/Q_28300077.html
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now