解析使用awk或sed的在ifconfig数据? [英] Parsing data from ifconfig with awk or sed?

查看:617
本文介绍了解析使用awk或sed的在ifconfig数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析在ifconfig输出一些数据SED,但我不能够正确地做到这一点。我希望命令只提取我后面的数字。

例如,我感兴趣的是提取发送的字节数:

  eth1的链接ENCAP:以太网的HWaddr 00:00:00:09:15:F7
      INET地址:192.168.1.2 BCAST:192.168.1.255子网掩码:255.255.255.0
      INET6地址:FE80 :: 92E2:BAFF:FE08:35c7 / 64范围:链接
      UP BROADCAST RUNNING MULTICAST MTU:1500跃点数:1
      RX数据包:75141错误:0丢弃:0超支:0帧:0
      TX数据包:78046错误:0丢弃:0超支:0载波:0
      碰撞:0 txqueuelen:1000
      RX字节数:9040489(9.0 MB)TX字节:34806464(34.8 MB)

如果我使用SED:

 使用ifconfig eth1的| SED的| * RX字节:\\([0-9] * \\)* | \\ 1 | G'

我得到这样的输出:

  eth1的链接ENCAP:以太网的HWaddr 00:00:00:09:15:F7
      INET地址:192.168.1.2 BCAST:192.168.1.255子网掩码:255.255.255.0
      INET6地址:FE80 :: 92E2:BAFF:FE08:35c7 / 64范围:链接
      UP BROADCAST RUNNING MULTICAST MTU:1500跃点数:1
      RX数据包:75141错误:0丢弃:0超支:0帧:0
      TX数据包:78046错误:0丢弃:0超支:0载波:0
      碰撞:0 txqueuelen:1000
9040627

不过,我只是在'9040627'感兴趣的是有一种用SED做到这一点,或者我应该用awk或其他的选择吗?

编辑:我使用的busybox二进制文件,所以我的选择是有限的。


解决方案

使用的grep

 使用ifconfig | grep的-OP(小于= RX字节:?)[0-9] *

使用 AWK

 使用ifconfig | awk的-F:'/ RX字节/ {打印$ 2 + 0}

I am trying to parse some data from ifconfig output with sed, but I am not able to do it correctly. I want the command to extract just the number I am after.

For example, I am interested in extracting the bytes sent:

eth1      Link encap:Ethernet  HWaddr 00:00:00:09:15:f7  
      inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::92e2:baff:fe08:35c7/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:75141 errors:0 dropped:0 overruns:0 frame:0
      TX packets:78046 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:9040489 (9.0 MB)  TX bytes:34806464 (34.8 MB)

If I use sed:

ifconfig eth1 | sed 's|.*RX bytes:\([0-9]*\).*|\1|g'

I get this output:

eth1      Link encap:Ethernet  HWaddr 00:00:00:09:15:f7  
      inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::92e2:baff:fe08:35c7/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:75141 errors:0 dropped:0 overruns:0 frame:0
      TX packets:78046 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
9040627

But I am only interested in '9040627' Is there a way to do it with sed, or should I use awk or other alternatives?

Edit: I am using busybox binaries, so my options are limited.

解决方案

use grep:

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

use awk:

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

这篇关于解析使用awk或sed的在ifconfig数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆