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 -Pzo '(?s)<ticket>[^<>]*?awesome3.*?</ticket>' file.txt
出力:
<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>
*?
- 非貪欲な修飾子