0

I want to show text between 3 words from text on 1 file:

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

I try this:

grep -Pzo '(?s)<ticket>.*\n.*awesome3.*\n.*</ticket>' file.txt

Result:

<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>

Expects:

<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>
1

Use the following grep approach:

grep -Pzo '(?s)<ticket>[^<>]*?awesome3.*?</ticket>' file.txt

The output:

<ticket>
BASH is awesome2
BASH is awesome3
texttext
</ticket>

*? - non-greedy modifier

  • Yeah thx! nice the non-greedy modifier. But why [^<>]? – PGrill Apr 13 '17 at 9:55
  • @PGrill, welcome, [^<>] - to ensure that there are no nested tags – RomanPerekhrest Apr 13 '17 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.