Can grep highlight matching text without hiding file content?
I frequently use grep
's context command line options (-A
, -B
, and -C
), but occasionally I want to view the whole file with matching expressions highlighted. To accomplish this currently I simply specify -C 999999999
, but this seems silly.
Is there an option within OS X's bundled grep
that will allow me to easily take advantage of grep's matching and colorizing features, without hiding any file content?
I don't think there is any simple option in grep but this seems to work fine. From https://stackoverflow.com/a/981831
grep --color -E "pattern|$" file
The |$
at the end of the regular expression makes grep find pattern OR end of lines. It can't highlight those though, so just the pattern gets colored.
Assuming you want to read the file yourself, with the bits highlighted, less is the better tool for the job. less yourfile
, type /
, your pattern and hit return. n
goes to the next match and space
to the next page.
Alternatively, ack has a --passthru
mode that does what you want. ack is so insanely useful it's worth looking into.