parsingawkiostat

Using AWK how can I total lines that match?


I'm trying to total the value of lines matching nvme* hhd from iostat in megabytes then awking to get total from line x to line z, in my case 2 lines.

iostat -m  <-- in megabytes

iostat -m |  awk '{if($1 ~ /nvme*/ ) print $2, $3, $4}'

------------------
9.38,   0.20,  0.57

13.67,  0.01,  1.60
------------------- 
23.05, 0.21, 2.17  <<-- The total

How can I achieve this?


Solution

  • Try:

    iostat -m |  awk '$1 ~ /nvme*/{a+=$2;b+=$3;c+=$4;print $2, $3, $4} END{print"--------------";print a,b,c}'
    

    How it works