Convince grep to output all lines, not just those with matches
172
votes
13
answers
85545
views
Say I have the following file:
$ cat test
test line 1
test line 2
line without the search word
another line without it
test line 3 with two test words
test line 4
By default,
grep
returns each line that contains the search term:
$ grep test test
test line 1
test line 2
test line 3 with two test words
test line 4
Passing the --color
parameter to grep
will make it highlight the portion of the line that matches the search expression, but it still only returns lines that contain the expression. Is there a way to get grep
to output every line in the source file, but highlight the matches?
My current terrible hack to accomplish this (at least on files that don't have 10000+ consecutive lines with no matches) is:
$ grep -B 9999 -A 9999 test test

If grep
can't accomplish this, is there another command-line tool that offers the same functionality? I've fiddled with [ack
](https://beyondgrep.com/) , but it doesn't seem to have an option for it either.
Asked by Michael Mrozek
(95525 rep)
Aug 12, 2010, 02:39 AM
Last activity: Mar 1, 2025, 03:11 AM
Last activity: Mar 1, 2025, 03:11 AM