hankknight
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:
I want the CPU value of processes to be combined and added togeather by command. For example:
ps.txt
# 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"}'
(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
}
}
Also, there should not be a comma after the last item.ps.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.