Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Linux: Calculating total value of numbers by command

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
ASKER CERTIFIED SOLUTION
Avatar of jb1dev
jb1dev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial