n.b. The following isn't really useful beyond a simple list of matching filenames as the reported matching line will almost certainly be the first line -- so Emacs can't trivially take you to the matching text.
GNU grep
supports the (non-POSIX) command line option -z
(or --null-data
), with the effect:
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline
So you can (most likely) match against your entire file with that, with some caveats:
- Output is also NUL-terminated, so you need to parse that.
- NUL bytes are already used in the normal output (when
grep-use-null-filename-separator
is enabled), so you may need to differentiate those.
- You likely don't want to see the full text of the matching file in the output.
The following will probably do the trick for post-processing the NUL chars.
perl -pe 's/\0(?![0-9]+:)/\n/g'
And the --only-matching
option will restrict the amount of displayed output.
Example using M-x grep
:
grep --only-matching --null-data --color -nH --null -e REGEXP FILES | perl -pe 's/\0(?![0-9]+:)/\n/g'