Linux awk 打印特定文本

Linux awk to print certain text

我有一条短信:

root@AngelBeats:~# ifconfig 3g-3g
3g-3g     Link encap:Point-to-Point Protocol
          inet addr:10.134.109.45  P-t-P:10.64.64.64  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:13416 errors:1018 dropped:0 overruns:0 frame:0
          TX packets:11517 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:6614985 (6.3 MiB)  TX bytes:2788421 (2.6 MiB)

我只想打印这些文本“6614985”和“2788421”(最后一行)我试过这些命令:

ifconfig 3g-3g | awk -F[\(\)] '/bytes/ {printf "%s",}'
ifconfig 3g-3g | awk -F[\(\)] '/bytes/ {printf "%s",}'

但是第一个命令打印 6.3 MiB 而第二个命令打印 2.6 MiB 如何只获取字节?请注意,当我浏览互联网时,字节数会不断变化。

ifconfig 3g-3g | grep bytes | nawk -F ":" '{print " "}' | nawk -F " " '{print " "}'

对于不同的输出来说不是很稳定,但是可以工作 ;)

总的来说:如果您只需要一行 grep 首先,这会使事情变得容易得多。

ifconfig 3g-3g | awk -F[:\(] '/bytes/ {printf "%s",}'

使用“:”作为字段分隔符就可以了

ifconfig 3g-3g | awk -F[:\(] '/bytes/ {printf "%s",}'
ifconfig 3g-3g | awk -F[:\(] '/bytes/ {printf "%s",}'

你可以试试这个:

ifconfig eth0|awk -F'[[:space:]:]*' '/RX bytes:/{print , }'

输出:

13738134 83649201

这会将两个值打印在一行中...也许您想将两个值都放入一个环境变量中。你可以这样做:

eval $(ifconfig eth0|awk -F'[[:space:]:]*' '/RX bytes:/{print "RX=", "TX="}')
echo $RX, $TX

输出:

13499630, 83579914

希望对您有所帮助!

只需要简单的grep命令和cut命令一起使用我们就可以这样实现,

$ ifconfig 3g-3g|grep "RX bytes"|cut -d'(' -f1|cut -d':' -f2                                                                             
$ ifconfig 3g-3g|grep "RX bytes"|cut -d'(' -f2|cut -d':' -f2