1

I have the following output of iperf version 2 running on LEDE OS. I am trying to parse the output to get the number before the Mbits/sec which is the average throughput of the IPERF session. However, it seems the separation between each column does not match certain number of spaces nor tabs. In addition, the CSV format generated by iperf generates strange results, as a result I have to rely on the regular output of iperf. Any suggestion how to parse the output using either regular expression or awk command?

The iperf command:

iperf -c 10.0.0.7 -t 10 -i 0.1 -f m

The output:

[  3] 0.00-10.00 sec  1889 MBytes  1584 Mbits/sec  15114/0          0     
2483K/3302 us
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

3 Answers3

3

You can use grep for those.

iperf -c 10.0.0.7 -t 10 -i 0.1 -f m | grep -o -E '\w+ Mbits/sec'

OR to be more accurate:

iperf -c 10.0.0.7 -t 10 -i 0.1 -f m | grep -o -E '[0-9]+ Mbits/sec'

To get only the digits, you can use yet another regex,

iperf -c 10.0.0.7 -t 10 -i 0.1 -f m | grep -Po '[[:digit:]]+ *(?=Mbits/sec)'

Above, [[:digit:]]+ and [0-9]+ are same and matches the digits in the line.

For FreeBSD grep in MacOS X, -P will not work. Instead use perl directly,

iperf -c 10.0.0.7 -t 10 -i 0.1 -f m | perl -nle 'print $& if m{\d+ *(?=Mbits/sec)}'
iamauser
  • 11,119
  • 5
  • 34
  • 52
  • It works but it prints Mbits/sec in the output. Any idea how to remove Mbits/sec? –  May 05 '17 at 15:33
  • There is a problem in the new command, it does not work: `Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...` –  May 05 '17 at 15:47
  • hmm, what grep version are you using ? – iamauser May 05 '17 at 15:49
  • BusyBox v1.26.2 () multi-call binary. –  May 05 '17 at 15:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143517/discussion-between-iot-and-iamauser). –  May 05 '17 at 16:01
0

You cat try to use awk tool :

iperf -c 10.0.0.7 -t 10 -i 0.1 -f m | awk -F 'MBytes' {'print $2'} 
0

If this is iperf 2 then try -fb for bit/byte formatting. This format is easier to parse w/regular expressions as it's just a number. Man page is here.

`GENERAL OPTIONS

-f, --format [abkmgBKMG] format to report: adaptive, bits, Bytes, Kbits, Mbits, Gbits, KBytes, MBytes, GBytes (see NOTES for more)`

rjmcmahon
  • 324
  • 1
  • 3