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?
Try:
iostat -m | awk '$1 ~ /nvme*/{a+=$2;b+=$3;c+=$4;print $2, $3, $4} END{print"--------------";print a,b,c}'
$1 ~ /nvme*/
This selects the lines whose first field matches the regex nvme*
. My iostat
produces no lines that contain nvme
. Consequently, it is up to you to determine if this is really the right regex for your case.
a+=$2;b+=$3;c+=$4
This keeps a running sum of the three columns of interest in the variables a
, b
, and c
.
print $2, $3, $4
This prints the three columns of interest.
END{print"--------------";print a,b,c}
After all lines have been read in, this prints out the total. The print"--------------"
produces a nice looking table on my system. If your iostat
produces output in a different format, you may need to adjust accordingly.