使用正则表达式仅提取带宽值

use*_*296 -2 python regex awk sed

如何从以下转储中提取仅带宽值

------------------------------------------------------------
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
Run Code Online (Sandbox Code Playgroud)

我的预期产量是

9.17,9.17,9.10,8.91,9.23,9.22,9.23,9.16,9.13,9.02,8.95
Run Code Online (Sandbox Code Playgroud)

我只是尝试用python做这个,但我希望知道其他更快更简单的方法来提取这个细节.

nu1*_*73R 5

awk 将是一个很好的候选人

$ awk -v ORS="," '$8 == "Gbits/sec"{print $7}' file
9.17,9.17,9.10,8.91,9.23,9.22,9.23,9.16,9.13,9.02,8.85,
Run Code Online (Sandbox Code Playgroud)

它能做什么?

  • -v ORS=","将输出记录分隔符设置为,.这使得每个print都被分开,.

  • '$8 == "Gbits/sec"检查第8列是否匹配Gbits/sec,如果是,则打印7字段,即带宽


如果你担心最后一个,,我们可以写,

$ awk -v ORS="" '$8 == "Gbits/sec"{print sep$7; sep=","} END{print "\n"}' file
Run Code Online (Sandbox Code Playgroud)

编辑

正如@ mklement0指出的那样,如果线路的起点像说的那样填满,这可能会失败[111].在这种情况下,我们可以重写为,

$ awk -v ORS="" '$NF == "Gbits/sec"{print sep$(NF-1); sep=","} END{print "\n"}' file
Run Code Online (Sandbox Code Playgroud)

这里NF是每行中的字段/列数.这$NF将是最后一个字段,$(NF-1)并将成为倒数第二列.