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
, but it doesn't seem to have an option for it either.
-C 9999
in place of-A 9999 -B 9999
.I always do:grep -C 9999 pattern file
-C9999
(or-C99
for a short input) might actually be high up on the list of elegance/availability, and gives the right return value if it finds a match. The only downside is it won't print anything if it doesn't find one.