Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get timestamps by line with iperf3 in bash script

Tags:

linux

bash

sed

I'm currently getting this output from iperf3

2016-03-03 21:33:50 [  4]   0.00-1.00 sec   113 MBytes   950 Mbits/sec      
2016-03-03 21:33:50 [  4]   1.00-2.00 sec   112 MBytes   941 Mbits/sec    0     
2016-03-03 21:33:50 [  4]   2.00-3.00 sec   113 MBytes   944 Mbits/sec    0

I want to create Graphics from this data, and as iperf3 can't update timestamps by line (as far as I know..) I'm looking for a way to increment the output file line by line.

result should be like:

2016-03-03 21:33:50 [  4]   0.00-1.00 sec   113 MBytes   950 Mbits/sec      
2016-03-03 21:33:51 [  4]   1.00-2.00 sec   112 MBytes   941 Mbits/sec    0     
2016-03-03 21:33:52 [  4]   2.00-3.00 sec   113 MBytes   944 Mbits/sec    0

so an action (+1) has to be done on each line containing Mbits/sec until the end of the file.

I guess that sed and/or date command may be helpful and a loop may be useful but can't see how to build it with time values..

like image 961
Remy Lannemajou Avatar asked Mar 03 '16 22:03

Remy Lannemajou


1 Answers

awk  '$10=="Mbits/sec"\
     {command="date -d "$2" +%s";command |getline $2;close(command)};1' 1txt \
    | awk -vi=1 '$10=="Mbits/sec"{$2=$2+i};i=i+1'\
    | awk '$10=="Mbits/sec"{command="date -d @"$2" +%T";command|getline $2;close(command)};1'

tested it on a file 1txt having values:

2016-03-03 21:33:50 [  4]   0.00-1.00 sec   113 MBytes   950 Mbits/sec      
2016-03-03 21:33:50 [  4]   1.00-2.00 sec   112 MBytes   941 Mbits/sec    0     
2016-03-03 21:33:50 [  4]   2.00-3.00 sec   113 MBytes   944 Mbits/sec    0
2016-03-03 21:33:50 [  4]   2.00-3.00 sec   113 MBytes   944 bits/sec    0

the output as expected after execution was:

2016-03-03 21:33:51 [ 4] 0.00-1.00 sec 113 MBytes 950 Mbits/sec
2016-03-03 21:33:52 [ 4] 1.00-2.00 sec 112 MBytes 941 Mbits/sec 0
2016-03-03 21:33:53 [ 4] 2.00-3.00 sec 113 MBytes 944 Mbits/sec 0
2016-03-03 21:33:50 [ 4] 2.00-3.00 sec 113 MBytes 944 bits/sec 0

P.S: you can ofcourse make it more compact and efficient by combining the awk's in a single command. But this helps in better understanding of whats going on.

like image 63
Inder Avatar answered Oct 12 '22 22:10

Inder