newSqlz
newSqlz

Reputation: 137

How to show only first match in pcregrep?

I have xml in log-files, that looks like:

<ServiceRs>
1
</ServiceRs>
text text text
<ServiceRs>
2
</ServiceRs>
text

So, i need to cut this XML from log file and i'm trying to do this with:

pcregrep -M '<ServiceRs>(\n|.)*</ServiceRs>'

But after this i didn't get two ServiceRs xml's, i got this:

<ServiceRs>
1
</ServiceRs>
text text text
<ServiceRs>
2
</ServiceRs>

I know, that i can modify pattern - (\n|.)* -> (\n|.){0, n), but i really don't know how many lines will be in xml.

Upvotes: 2

Views: 700

Answers (1)

Abhilash Nayak
Abhilash Nayak

Reputation: 704

Can you try

pcregrep -M '<ServiceRs>(\n|.)*?</ServiceRs>'

? is for lazy match

It only matches the content between the <ServiceRs> and </ServiceRs> and excludes the rest text text...s

Ref: https://regexr.com/3h1ug

Upvotes: 1

Related Questions