Suppose we are doing a multiline regex pattern search on a bunch of files and we want to extract the matches from grep. By default, grep outputs matches separated by newlines, but since we are doing multiline patterns this creates the inconvenience that we cannot easily extract the individual matches.
Example
grep -rzPIho '}\n\n\w\w\b' | od -a
Depending on the files in your filetree, this may yield an output like
0000000 } nl nl m y nl } nl nl i f nl } nl nl m
0000020 y nl } nl nl m y nl } nl nl i f nl } nl
0000040 nl m y nl
0000044
As you can see, we cannot split on newlines to obtain the matches for further processing, since the matches contain newline characters themselves.
What doesn't work
Now the --null
(or -Z
) only works in conjunction with -l
, which makes grep only list filenames instead of matches, so that doesn't help here.
Note, this is not a duplicate of Is there a grep equivalent for find's -print0 and xargs's -0 switches?, because the requirements in that question are different, allowing it to be answered using alternative techniques.
So, how can we make this work? Maybe use grep in conjuction with other tools?