简体   繁体   English

使用sed使用一个表达式匹配多个模式,然后删除直到空白行

[英]Using sed to match on multiple patterns with one expression, and delete until a blank line

On a RHEL 6.6 system, using ifconfig and GNU sed, I want to display only the Ethernet interfaces which aren't logical sub interfaces, or the loopback. 在RHEL 6.6系统上,使用ifconfig和GNU sed,我只想显示不是逻辑子接口的以太网接口或环回。

For example, the output should not contain interface records where the interface name is like eth0:134 or lo. 例如,输出不应包含接口名称如eth0:134或lo的接口记录。

My approach so far has been to use sed with two expressions, The first, /eth[0-9]:/ to match on and include all lines containing 'ethN:, including every line after until a blank line is encountered, and delete, and a second expression to match on, /lo/ and all lines after until a blank line, and delete them as well. 到目前为止,我的方法是使用sed和两个表达式,第一个, /eth[0-9]:/匹配并包含所有包含'ethN:的行,包括直到遇到空行之后的每一行,然后删除,以及第二个要匹配的表达式,/ lo /和之后的所有行,直到空白行,然后将其删除。

For example: 例如:

[user@system ~]$ ifconfig -a | sed '/eth[0-9]:/,/^$/d; /lo/,/^$/d'


eth0     Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.50 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
         RX packets:1024 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
         TX packets:2048 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
         collisions:0 txqueuelen:1000
         RX bytes:6455319 (6.1 MiB)  TX bytes: 258478  (252.4 KiB)

Un-desired output looks like: 不需要的输出看起来像:

eth0:146 Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.51 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth0:147 Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.52 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric

eth0:148 Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.53 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric

lo       Link encap:Local Lookback
         inet addr:127.0.0.1 Mask:255.0.0.0
         UP LOOPBACK RUNNING MTU:16436 Metric:1
         RX packets:605 errors:0 dropped:0 overruns:0 frame:0
         TX packets:605 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:59008  (57.6 KiB)  TX bytes:59008  (57.6 KiB)

I like this method of deleting all lines of output starting at and including the matched line until a blank line (^$) is encountered because there are a variable number of extra lines after the line containing the interface name. 我喜欢这种删除所有输出行的方法,该方法从匹配的行开始并包括匹配的行,直到遇到空行(^ $),因为在包含接口名称的行之后,有可变数量的额外行。 Either 2, additional, or 6 additional lines in this case. 在这种情况下,可以是2行或6行。

This method allows there to be N additional lines of output as long as a blank line is still used as a separator between displayed interface records. 只要空白行仍用作显示的接口记录之间的分隔符,此方法就可以有N条额外的输出行。

How can the second expression, /lo/,/^$/d' be combined with the first? 第二个表达式/lo/,/^$/d'如何与第一个表达式组合?

Perhaps another approach to how the lines are matched (or not matched) is better? 也许另一种方式来匹配(或不匹配)行是更好的?

Another issue is that this only matches the first 10 interfaces. 另一个问题是,这仅与前10个接口匹配。 There aren't more than 10, but it would be good to account for that in case there are. 数量不超过10个,但如果有的话,最好考虑一下。

I'd like to match on the first 100 interfaces with something like: 我想在前100个接口上匹配以下内容:

^[1-9][0-9]?$|^100$

Solutions using awk are ok as well. 使用awk的解决方案也可以。

Try: 尝试:

ifconfig -a | sed -r '/(eth[0-9]{1,2}:|eth100:|lo)/,/^$/d'

{1,2} means one or two of the preceding. {1,2}表示前述中的一个或两个。 So, eth[0-9]{1,2} matches eth followed by one or two numbers. 因此, eth[0-9]{1,2}匹配eth后跟一个或两个数字。

(A|B|C) matches either A or B or C . (A|B|C)匹配ABC So, (eth[0-9]{1,2}:|eth100:|lo) matches either eth with one or two numbers or eth100 and a colon or lo . 因此, (eth[0-9]{1,2}:|eth100:|lo)匹配具有一个或两个数字的eth eth100和一个冒号 lo

The used -r for extended regular expressions (ERE). -r用于扩展正则表达式(ERE)。 Without -r , sed defaults to basic regular expressions (BRE). 如果不使用-r ,则sed缺省为基本正则表达式(BRE)。 ON GNU sed, BRE work the same but at the cost of extra backslashes: 在GNU sed上,BRE的工作原理相同,但以额外的反斜杠为代价:

ifconfig -a | sed '/\(eth[0-9]\{1,2\}:\|eth100:\|lo\)/,/^$/d'

BSD/OSX BSD / OSX

BSD (OSX) sed does not recognize the -r option. BSD(OSX)sed无法识别-r选项。 To get extended regex, use -E instead: 要获取扩展的正则表达式,请改用-E

ifconfig -a | sed -E '/(eth[0-9]{1,2}:|eth100:|lo)/,/^$/d'

-E will also work with recent versions of GNU sed . -E也可以与GNU sed最新版本一起使用。

It sounds like all you need is: 听起来您需要做的只是:

awk -v RS= -v ORS='\n\n' '$1~/^eth[0-9]+$/'

eg: 例如:

$ cat file
eth0:146 Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.51 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth0     Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.50 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
         RX packets:1024 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
         TX packets:2048 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
         collisions:0 txqueuelen:1000
         RX bytes:6455319 (6.1 MiB)  TX bytes: 258478  (252.4 KiB)

eth0:147 Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.52 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric

eth0:148 Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.53 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric

lo       Link encap:Local Lookback
         inet addr:127.0.0.1 Mask:255.0.0.0
         UP LOOPBACK RUNNING MTU:16436 Metric:1
         RX packets:605 errors:0 dropped:0 overruns:0 frame:0
         TX packets:605 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:59008  (57.6 KiB)  TX bytes:59008  (57.6 KiB)

.

$ awk -v RS= -v ORS='\n\n' '$1~/^eth[0-9]+$/' file
eth0     Link encap:Ethernet HWaddr 00:11:22:33:44:55
         inet addr:192.168.0.50 Bcast: 192.168.0.255 Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
         RX packets:1024 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
         TX packets:2048 ERRORS:0 DROPPED:0 OVERRUNS:0 FRAME:0
         collisions:0 txqueuelen:1000
         RX bytes:6455319 (6.1 MiB)  TX bytes: 258478  (252.4 KiB)

If you only want to match interface numbers 0 to 100 just tweak it to: 如果您只想匹配接口编号0到100,则将其调整为:

awk -v RS= -v ORS='\n\n' '$1~/^eth([1-9]?[0-9]|100)$/'

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

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