Solved

Linux: skip if no item matches

Posted on 2013-11-20
1
334 Views
Last Modified: 2013-11-21
I use the following code to identify the top 200 CPU-hogging processes and return results in JSON format:
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

(See the attached file, ps.txt)

It works great but if there are no matching results then I want it to return nothing at all.  No JSON should be output unless there is at least one resource with a value greater than 0.
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
1 Comment
 
LVL 9

Accepted Solution

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

Open in new window


I have changed you code slightly. In the first awk, I supressed the empty line which was printed when the input is empty and in the second awk I suppressed all output when no input is coming by adding a status variable s. Therefore I had to move the output in the BEGIN into the middle part of the awk script, because in inside BEGIN you can not check for an empty input.

Oli
0

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.
Suggested Courses

630 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