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

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .