0

Im cutting CPU usage statistics within a bash file and presenting it formatted, from iostat there are multiple fields but only user, system and idle are relevant

as shown in the following:

echo "" `iostat -c | awk 'NR==3' | cut -d '%' -f 1,2,4,7`
echo "" `iostat -c | awk 'NR==4' | cut -d '   ' -f 1,2,4,7`

the current output is as follows:

 avg-cpu: %user %system %idle
cut: the delimiter must be a single character
Try 'cut --help' for more information.

When im using this to cut the fields it does not work with the next line because the spacing is differing within the fields, how do you account for this as it only allows for a single character when cutting a delimiter?

here is the command looks when regularly executed without formatting:

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.07    0.01    0.14    0.04    0.00   99.74
null.ts
  • 33
  • 1
  • 6

3 Answers3

3

You don't need to write it twice, all you need is one awk:

iostat -c | awk 'NR==3{print $1,$2,$4,$7};NR==4{print $1,$3,$6}'
Quasímodo
  • 3,812
  • 14
  • 25
0

You can format the output with printf in awk and print the interested fields:

 iostat -c | awk '/avg-cpu/{printf "%8s %8s %8s %8s\n", $1, $2, $4, $7; getline; printf "         %8s %8s %8s\n", $1, $3, $6}'
P.P
  • 117,907
  • 20
  • 175
  • 238
0

The -d argument of the cut command accepts ' ' as the delimiter for one or more spaces. You can remove the leading spaces, for example, using this. The following command gives you 0.07 0.14 99.74 as output:

echo "" `iostat -c | awk 'NR==4' | awk '{$1=$1};1' | cut -d' ' -f1,3,6`
cesarsotovalero
  • 1,116
  • 6
  • 15