?
Solved

Linux: Only include items when value is more than 0

Posted on 2013-11-20
3
Medium Priority
?
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 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