Assuming mpstat-log.txt file below, how can I parse the results of a file and get the %idle field for the first 4 CPUs 0-3. My guess is this is a job for grep, sed, cut, or awk but I am not experienced enough to know which is the best utility(s) to use for the job.
#!/bin/bash
for cpuIndex in {0..3}
do
# Get percent idle from line of specified cpuIndex
cpuPercentIdle= # How to get a desired line and space separated field number?
# Can you do math in a bash script ?
cpuPercentUsed=$((100 - $cpuPercentId))
echo "CPU $cpuIndex : Idle=$cpuPercentIdle, Used=$cpuPercentUsed"
done
done
Script Output:
CPU 0 : Idle=45.18, Used=54.82 CPU 1 : Idle=96.33, Used=3.67 CPU 2 : Idle=95.65, Used=4.35 CPU 3 : Idle=72.09, Used=27.91
File:mpstat-log.txt
Average: CPU %user %nice %sys %iowait %irq %soft %steal %idle intr/s Average: all 10.95 0.00 0.42 0.00 0.04 0.25 0.00 88.34 1586.71 Average: 0 51.50 0.00 3.32 0.00 0.00 0.00 0.00 45.18 0.00 Average: 1 3.67 0.00 0.00 0.00 0.00 0.00 0.00 96.33 2.66 Average: 2 4.35 0.00 0.00 0.00 0.00 0.00 0.00 95.65 0.00 Average: 3 27.57 0.00 0.33 0.00 0.00 0.00 0.00 72.09 997.34 Average: 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 10.96 Average: 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 Average: 6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 Average: 7 0.00 0.00 0.00 0.00 0.00 1.67 0.00 98.33 575.75
Thanks in advance for any guidance and tips!