我正在尝试获取此命令的一些特定行:
ifconfig
这是命令的输出:
eth0 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BC
inet addr:10.71.1.30 Bcast:10.71.1.255 Mask:255.255.255.0
inet6 addr: fe80::210:e0ff:fe3f:6fbc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3059275068 errors:0 dropped:1378 overruns:0 frame:0
TX packets:2094779962 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6566542892239 (5.9 TiB) TX bytes:202791652910 (188.8 GiB)
eth1 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BD
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:1417584931 errors:0 dropped:32908 overruns:0 frame:0
TX packets:1284691038 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2256566826674 (2.0 TiB) TX bytes:182643225952 (170.0 GiB)
我只想要包含“Link”和“bytes”字样的行,例如:
eth0 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BC
RX bytes:6566542892239 (5.9 TiB) TX bytes:202791652910 (188.8 GiB)
eth1 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BD
RX bytes:2256566826674 (2.0 TiB) TX bytes:182643225952 (170.0 GiB)
最佳答案
最好的方法必须是使用 grep
来过滤行。例如,使用:
ifconfig | egrep " Link|bytes"
请注意,我在 Link
之前添加了一个空格,以避免匹配以 Scope:Link
结尾的行。
您还可以使用:
ifconfig | awk '/ Link/ || /bytes/'
或
ifconfig | grep " Link\|bytes"
关于linux - 如何在 Linux 中跨多行查找模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22038110/