【问题标题】:How to get the paragraph contain the key words in shell scripts?如何让段落包含shell脚本中的关键词?
【发布时间】:2012-08-15 00:01:39
【问题描述】:

我想得到包含关键词的整个段落。

例如下面是"ifconfig -a"的输出

bond0     Link encap:Ethernet  HWaddr 00:11:3F:C1:47:98  
          inet6 addr: fe80::211:3fff:fec1:4798/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:1881856 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1059020 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2618747813 (2.4 GiB)  TX bytes:182058226 (173.6 MiB)

bond0:oam Link encap:Ethernet  HWaddr 00:11:3F:C1:47:98  
          inet addr:135.2.156.97  Bcast:135.2.156.111  Mask:255.255.255.240
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

bond0:oamA Link encap:Ethernet  HWaddr 00:11:3F:C1:47:98  
          inet addr:135.2.156.103  Bcast:135.2.156.111  Mask:255.255.255.240
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

我想提取粗体的段落。即段落包含关键词"bond0:oamA" 我知道如果我使用 grep,只有这行

bond0:oamA Link encap:Ethernet  HWaddr 00:11:3F:C1:47:98 

会得到的。 但我想提取整段包含关键词。

有没有办法得到这一段?

非常感谢!

【问题讨论】:

标签: shell awk grep cut


【解决方案1】:

试试这个命令:

ifconfig -a | awk -vRS='' '$1~/bond0:oamA/'

RSnull 时,awk 将文件解析为多行记录。

【讨论】:

  • 非常感谢!它对我帮助很大!
猜你喜欢
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2017-12-07
  • 1970-01-01
  • 2015-06-08
  • 2016-09-18
  • 2013-07-05
相关资源
最近更新 更多