1

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!

3 Answers 3

3

Just about all the tools you mention can do the job one way or another. Here's one with awk...

awk '{ if ( $2 <= 3 ) print $10; }' mpstat-log.txt

Sign up to request clarification or add additional context in comments.

1 Comment

The more awk-ish way to write this is: awk '$2 <= 3 {print $10}'
2

Solution using awk:

{ if($2 ~ /[0-3]/) {
    print $10
  }
}

gives:

$ awk -f t.awk input
45.18
96.33
95.65
72.09

In pure bash, something like this perhaps:

#!/bin/bash

while read -a ARRAY
do
    if [[ "${ARRAY[1]}" =~ [0-3] ]]; then
        echo ${ARRAY[9]}
    fi
done < input

gives:

45.18
96.33
95.65
72.09

Bash cannot to floating point arithmetic, for that you can use bc. See this link for how you can use bc to do the math for you.

3 Comments

If there are > 10 CPUs, the regex /[0-3]/ will match too many
@glenn jackman - true, limit it to a single digit using /^[0-3]$/ instead.
Thank you all! The stuff that is possible to cobble together with grep, awk, sed, etc. makes my head spin. While very powerful, I feel sorry for the next Linux newbie that comes along to try and maintain. I guess I just need to leave descriptive comments.
1
$ awk '$2~/^[0-3]$/ {printf "CPU %d : Idle=%.2f, Used=%.2f\n",$2,$10,100-$10}' mpstat-log.txt
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

2 Comments

If there are > 10 CPUs, the regex /[0-3]/ will match too many
@glenn good point. Updated answer to cover more cases. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.