Solved

Linux: Calculating total value of numbers by command

Posted on 2013-11-20
1
236 Views
Last Modified: 2013-11-20
I use the following code to identify the top 3 CPU-hogging processes and return results in JSON format:
# Thanks, simon3270
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 "{\n \"hogs\": {"}NR <= 3{printf "   \"%s\" %.1f,\n",$1,$2/10}END{print " }\n}\n"}'

Open in new window

(See the attached file, ps.txt)

I want the CPU value of processes to be combined and added togeather by command.  For example:
{
 "hogs": {
   "httpd" 9,
   "ps" 0.7,
   "init" 0.1
 }
}

Open in new window

Also, there should not be a comma after the last item.
ps.txt
0
Comment
Question by:hankknight
1 Comment
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39664034
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 "{\n \"hogs\": {"}NR <= 3{printf "   \"%s\" %.1f",$1,$2; if ( NR != 3 ) { printf ",\n" } else { printf "\n" }  } END{print " }\n}\n"}'

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
App holding yum lock unable to update my rpm package 1 36
Shell Script on AIX 7 64
Reset Root Password on CentOS 6 4 44
expectj telnet failing 5 26
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…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now