linux - 如何在 Linux 中跨多行查找模式?

标签 linux unix

我正在尝试获取此命令的一些特定行:

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/

相关文章:

php - 如何在 Debian 上使用 Concrete5 解决 "Pragma: no-cache"问题?

c - 从 C 中的输入重定向读取整数

c - 将 waitpid 与 WNOHANG 一起使用时的错误状态

c - 为什么 `accept(2)` 需要 `sockaddr` 长度作为单独的指针?

linux - 从一行中提取多个 float

linux - MongoDB 不运行

unix - 如何将文件从linux复制到windows

c - 如何清除 C 中的屏幕?

linux - Bash:消除冗余路径组件?

linux - 为什么有些嵌入式板需要设备树而有些则不需要?