1

Referring to question below, it seems that pcregrep does not narrow the result to the first occurance but expands to the maximum extent possible. So if for e.g. the content is:

blah blah..
blah blah..
blah abc blah
blah blah..
blah blah..
blah blah..
blah efg1 blah blah
blah blah..
blah blah..
blah efg2 blah blah
blah blah..
blah blah..

the output goes up to efg2, while I would expect to go only until efg1, the latter being the first occurance. Any ideas how I can achieve this?

How to find patterns across multiple lines using grep?

1 Answers1

0

Use

pcregrep -M  '(?s)abc.*?efg' test.txt

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (?s)                     set flags for this block (with . matching
                           \n) (case-sensitive) (with ^ and $
                           matching normally) (matching whitespace
                           and # normally)
--------------------------------------------------------------------------------
  abc                      'abc'
--------------------------------------------------------------------------------
  .*?                      any character (0 or more times (matching
                           the least amount possible))
--------------------------------------------------------------------------------
  efg                      'efg'
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37