Search code examples
grepiperf

Capturing certain value from iperf result using grep


I use iperf3 version and then I get the performance result like this in terminal :

[  4] local 10.0.1.8 port 34355 connected to 10.0.1.1 port 5201
49,49,0,0,35500
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec  2.19 MBytes  18.4 Mbits/sec    0   69.3 KBytes       
CPU Utilization: local/sender 2.8% (0.7%u/3.3%s), remote/receiver 1.4% (0.6%u/0.9%s)

I want to use only certain values which I will use in the bash script later. What I want is like this :

35500,18.4,2.8

As far as I know I can use grep to print bandwidth only :

./src/iperf3 -c 10.0.1.1 -d -t 1 -V | grep -Po '[0-9.]*(?= Mbits/sec)'

but is it possible to obtain "35500,18.4,2.8" using grep and how to do it?

Thank you for the answers


Solution

  • grep with P(Perl-regex) option allows you to include multiple regexes,

    $ grep -Po '(?<=,)[0-9]+$|[0-9.]*(?= Mbits/sec)|(?<=local\/sender )[^%]*' file | paste -d, - - -
    35500,18.4,2.8
    

    So your command would be,

    $ ./src/iperf3 -c 10.0.1.1 -d -t 1 -V | grep -Po '(?<=,)[0-9]+$|[0-9.]*(?= Mbits/sec)|(?<=local\/sender )[^%]*' | paste -d, - - -