user2532296 user2532296 - 16 days ago 9
Python Question

extracting just the bandwidth value using regex

How can I extract the just the Bandwidth value from the following dump

------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[ 4] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51725
[ ID] Interval Transfer Bandwidth
[ 4] 0.0-10.0 sec 10.7 GBytes 9.17 Gbits/sec
[ 5] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51726
[ 5] 0.0-10.0 sec 10.7 GBytes 9.17 Gbits/sec
[ 4] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51727
[ 4] 0.0-10.0 sec 10.6 GBytes 9.10 Gbits/sec
[ 5] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51728
[ 5] 0.0-10.0 sec 10.4 GBytes 8.91 Gbits/sec
[ 4] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51729
[ 4] 0.0-10.0 sec 10.8 GBytes 9.23 Gbits/sec
[ 5] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51730
[ 5] 0.0-10.0 sec 10.7 GBytes 9.22 Gbits/sec
[ 4] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51731
[ 4] 0.0-10.0 sec 10.7 GBytes 9.23 Gbits/sec
[ 5] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51732
[ 5] 0.0-10.0 sec 10.7 GBytes 9.16 Gbits/sec
[ 4] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51733
[ 4] 0.0-10.0 sec 10.6 GBytes 9.13 Gbits/sec
[ 5] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51734
[ 5] 0.0-10.0 sec 10.5 GBytes 9.02 Gbits/sec
[ 4] local 192.168.1.1 port 5001 connected with 192.168.1.2 port 51735
[ 4] 0.0-10.0 sec 10.3 GBytes 8.85 Gbits/sec


My expected output is

9.17,9.17,9.10,8.91,9.23,9.22,9.23,9.16,9.13,9.02,8.95


I only have tried to do this with python, but I wish to know other faster and simpler approaches to do extract this detail.

Answer

With grep if pcre option is available

$ grep -oP '[0-9.]+(?=\s*Gbits)' ip.txt | paste -sd,
9.17,9.17,9.10,8.91,9.23,9.22,9.23,9.16,9.13,9.02,8.85
  • [0-9.]+ numbers to extract
  • (?=\s*Gbits) only if it is followed by optional spaces and the text Gbits
  • then use paste command to combine the output to single line with , as delimiter


From man grep and man paste

grep, egrep, fgrep, rgrep - print lines matching a pattern

-o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-P, --perl-regexp Interpret the pattern as a Perl-compatible regular expression (PCRE). This is highly experimental and grep -P may warn of unimplemented features.

paste - merge lines of files

-s, --serial paste one file at a time instead of in parallel

-d, --delimiters=LIST reuse characters from LIST instead of TABs