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.
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.
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?
So I filed this issue as a feature request in the GNU grep bug mailing list, and it appeared to be a bug in the code.
It has been fixed and pushed to master, so it will be available in the next release of GNU grep: http://git.savannah.gnu.org/cgit/grep.git/commit/?id=cce2fd5520bba35cf9b264de2f1b6131304f19d2
To summarize: this patch makes sure that the -z
flag not only works in conjunction with -l
, but also with -o
.
What comes into my mind would be to use a group separator, for example something like:
grep -rzPIho '}\n\n\w\w\b' $FILE -H | sed "s/^$FILE:/\x0/"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With