Remy Lannemajou
Remy Lannemajou

Reputation: 61

Get timestamps by line with iperf3 in bash script

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

Upvotes: 6

Views: 1301

Answers (2)

Inder
Inder

Reputation: 3826

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.

Upvotes: 2

simhumileco
simhumileco

Reputation: 34643

You can do this using sed, but this is not trivial... It is much easier to do it using perl:

perl -lne 'print $1.($2 + ($.) - 1).$3 if /(.+)(50)(.+)/'  file.txt
  • -l enable line ending processing, specifies line terminator
  • -n assume loop around program
  • -e one line of program
  • print print command
  • . string concatenation
  • $number variables contain the parts of the string that matched the capture groups ()
  • $. the current record number
  • ($2 + ($.) - 1) means: 50 + 'current record number' - 1
  • if /(.+)(50)(.+)/' statement with regular expression referred to by print
  • file.txt file with your datas

Upvotes: 1

Related Questions