Solved

Linux: Only include items when value is more than 0

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

773 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