Solved

Linux: Only include items when value is more than 0

Posted on 2013-11-20
3
312 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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Hosting 16 80
000webhost.com default error log 1 62
RHEL 6.7 Gnome Desktop on VMware 6 VM 9 115
Windows 10 linux VM 22 47
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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

730 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