Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux: skip if no item matches

Posted on 2013-11-20
1
Medium Priority
?
337 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
1 Comment
 
LVL 9

Accepted Solution

by:
oheil earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
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.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month7 days, 3 hours left to enroll

783 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