Based solely on your posted iperf
output, you would simply print fields 3 and 4 from the last line:
| awk 'END{print $3" "$7}'
BUT this question is not simply about awk
.
Question:
Let's start by explaining what your iperf
command does, for the benefit of those who are not familiar with it.
-c 10.0.0.1
connects to server listening at address 10.0.0.1
-t 3600
keeps running for 3600
seconds, i.e. 1 hour
-i 2
prints a report every 2
seconds
During the next hour, a line like this is printed (added) on screen every two seconds:
[ 3] 0.0-10.0 sec 19.7 MBytes 15.8 Mbits/sec
What you want is reduce the information printed on screen every two seconds, to lines of the form:
0.0-10.0 15.8
Answer: To obtain this, you pipe the output to awk
:
iperf -c 10.0.0.1 -t 3600 -i 2 | awk '/[0-9]]/{sub(/.*]/,"");print $1" "$5}'
You can stop reading here if you employ iperf
, i.e. iperf 2.
If you use iperf3
, you should have found out by now that the piped output gets buffered. This means you do not see a new line printed on screen every two seconds! Instead, a whole bunch of lines gets printed every 50 seconds or so, depending on the system. Note that this does not happen with iperf 2 because it flushes the output after printing each line.
To have a line of the desired form printed on screen at the desired interval, you need --forceflush
:
--forceflush
force flushing output at every interval. Used to avoid buffer-
ing when sending output to pipe.
Answer:
iperf3 -c 10.0.0.1 -t 3600 -i 2 --forceflush | awk '/[0-9]]/{sub(/.*]/,"");print}'
Note that this option was added to iperf3
version 3.1.5
.