1

I am implementing a multiline search in UNIX using 'pcregrep'. I want the matching to stop at the 1st occurrence.

e.g. if i have below text in a file:

mynameishin
hahahain
internetin
fdhsufsdhuiain
djbssdvbsbsduiain
sduigsyubsuvasdyivsyifvasin

I want to print till the 1st occurrence of 'ain' after 'name'. So i need to print only:

mynameishin
hahahain

I am executing:

pcregrep -M 'name.*ain' *.txt

However I am getting the output as:

mynameishin
hahahain
internetin
fdhsufsdhuiain
djbssdvbsbsduiain
sacs0406
  • 19
  • 1

1 Answers1

6
pcregrep -M 'name.*?ain' *.txt

Make it non greedy!!!!!!!!!!!.* is greedy.it will consume all and stop at end.

vks
  • 67,027
  • 10
  • 91
  • 124