1

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?

Toto
  • 89,455
  • 62
  • 89
  • 125
Queasy
  • 131
  • 11
  • Just add the numbers as you see them to variables keeping the total and print those out in an `END` block. – Etan Reisner Feb 18 '15 at 20:25
  • Can you please clearly post your source data, and an example of the output you're trying to achieve? The `iostat` command on my systems does not produce output that includes "nvme". – ghoti Feb 18 '15 at 20:25
  • Etan, that's the question: How is it done with variable? It's a different value per line so how are both lines totaled as a variable. I've tried several ways and doesn't compute. – Queasy Feb 18 '15 at 20:32
  • Ghoti, that is the source. Not sure what you mean by "does not produce output". It correctly reports the interface from iostat and that isn't the issue anyway, I'm trying to get totals. The command works perfectly in other words. – Queasy Feb 18 '15 at 20:37
  • 1
    @Queasy, on my system, the `iostat` command does not have a `-m` option. If you post a question that depends on specific input, you'll get better answers if you actually provide the test input you're trying to process. – ghoti Feb 18 '15 at 20:38

1 Answers1

4

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

  • $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.

John1024
  • 109,961
  • 14
  • 137
  • 171