我想显示 1 个文件中文本中 3 个单词之间的文本:
lalala
<ticket>
BASH is awesome
BASH is awesome1
</ticket>
lalalala
<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>
lololo
<ticket>
awesome
BASH is awesome4
BASH is awesome5
</ticket>
lalalla
我试试这个:
grep -Pzo '(?s)<ticket>.*\n.*awesome3.*\n.*</ticket>' file.txt
结果:
<ticket>
BASH is awesome
BASH is awesome1
</ticket>
lalalala
<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>
lololo
<ticket>
awesome
BASH is awesome4
BASH is awesome5
</ticket>
期望:
<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>
答案1
使用以下内容grep方法:
grep -Pzo '(?s)<ticket>[^<>]*?awesome3.*?</ticket>' file.txt
输出:
<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>
*?
- 非贪婪修饰符