对于我的一个脚本我需要在2个模式之间打印文本,如果在里面找到匹配,我不知道如何使它变得简单。
文件的内容是:
===== seble dom0 report =====
IP address: 10.42.0.100
location: slot-3.enclosure-43.eqx
ID: infra-dom0.dom0.seble
Xen-Version: 4.4
CPU: Intel(R) Xeon(R) CPU E5-2660 v3 @ 2.60GHz
===== arnica dom0 report =====
IP address: 10.1.42.46
location: slot-3.enclosure-12.eqx
ID: infra-dom0.dom0-3
Xen-Version: 4.1
CPU: AMD Opteron(tm) Processor 6174
===== sithtemd dom0 report =====
IP address: 10.1.42.191
location: slot-13.enclosure-7.vty
ID: infra-dom0.mutu119
Xen-Version: 4.4
CPU: Intel(R) Xeon(R) CPU X5670 @ 2.93GHz
如果我为了例子搜索enclosure-7,我希望它返回:
===== sithtemd dom0 report =====
IP address: 10.1.42.191
location: slot-13.enclosure-7.vty
ID: infra-dom0.mutu119
Xen-Version: 4.4
CPU: Intel(R) Xeon(R) CPU X5670 @ 2.93GHz
它是grep和sed -n "/===== /,/^$/p"
之间的混合,但无法找到它......
提前感谢您的回答:)
答案 0 :(得分:1)
这就是答案:
perl -0777 -lne 'print for grep /enclosure-7/, /^=====.*?^$.*?\n/mgs' file.txt
答案 1 :(得分:1)
p
将被视为regular expression
awk -v p='enclosure-7' -v RS= '$0~p' file.dat
Empty RS means that records are separated by one or more blank lines and nothing else.
答案 2 :(得分:0)
使用sed:
sed '/^=====/{:a;N;/\n$/!ba;/enclosure-7/!d}' file