Parsing data from ifconfig with awk or sed?

对着背影说爱祢 提交于 2019-12-01 00:40:43

use grep:

ifconfig | grep -oP '(?<=RX bytes:)[0-9]*'

use awk:

ifconfig | awk -F: '/RX bytes/{print $2+0}'

IMHO there is no standard for the ifconfig - output. It (may) change from system to system and from release to release.

If I were you, I would go for the /sys file system. You get all the information also from there - without the need of post-processing.

$ cat /sys/class/net/eth0/statistics/rx_bytes
37016050

By default, sed prints out each line of the input, after any changes you've made to the line. Since you only want to print out something from the line with "RX bytes", you tell sed not to print every line (-n). So you want to specify the range on which the substitution should be performed, only the line that matches RX bytes, and then do the substitution and explicitly print the results.

ifconfig eth1 | sed '/RX bytes/{s|.*RX bytes:\([0-9]*\).*|\1|; p}'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!