Search code examples
bashpipecutiostat

Bash - delimeter to cut field differing spacing


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

Solution

  • 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}'