简体   繁体   English

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

[英]How to find patterns across multiple lines in Linux?

i'm trying to get some specific lines of this command: 我正在尝试获取此命令的一些特定行:

ifconfig

here is the output of the command: 这是命令的输出:

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)

I just want lines that contains "Link" and "bytes" words, for example: 我只想要包含“链接”和“字节”字样的行,例如:

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)

Te best approach must be to use grep to filter the lines. 最好的方法必须是使用grep来过滤行。 For example, use: 例如,使用:

ifconfig | egrep " Link|bytes"

Note I added a space before Link to avoid matching the line ending with Scope:Link . 注意我在Link之前添加了一个空格,以避免匹配以Scope:Link结尾的行。

You can also use: 您还可以使用:

ifconfig | awk '/ Link/ || /bytes/'

or 要么

ifconfig | grep " Link\|bytes"

Of course grep is the best tool for this. 当然, grep是实现此目的的最佳工具。 But, there are some other ways are available to do the same. 但是,还有其他一些方法可以执行相同操作。 That is, 那是,

ifconfig | sed -n '/Link \|bytes/p'

and

ifconfig | awk '/Link |bytes/'
ifcofig|perl -lne 'print if(/Link/||/bytes/)'

Here's a couple of useful patterns that most people I've met don't seem to know about. 这是我遇见的大多数人似乎都不了解的几个有用的模式。 The first is particularly relevant to your problem. 第一个与您的问题特别相关。

grep -e is your best new-found friend grep -e是您最好的新朋友

ifconfig | grep -e Link -e bytes

You can do a similar thing with sed, which has an additional bonus of also printing out the first line (which might contain a heading). 您可以使用sed做类似的事情,它还具有打印第一行(可能包含标题)的额外好处。 In this example, I'm printing out the heading line and any lines contain LISTEN or 'bar' (The second expression is just there to reinforce the fact that you're not limited to one.) Here, the 1p is addressing the first line, (origin-1), with the operation to 'p'rint 在此示例中,我正在打印标题行,并且任何行都包含LISTEN或'bar'(第二个表达式只是为了强调这一事实,即您不限于一个。)在这里,1p指向第一个行(origin-1),对'p'rint进行操作

lsof -Pni | sed -n -e 1p -e '/LISTEN/p' -e '/bar/p'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM