Encourage grep to result all lines, not simply those with suits

Claim I have the adhering to documents:

$ 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 which 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 certainly make it highlight the section of the line that matches the search expression, yet it still just returns lines which contain the expression. Exists a means to get grep to result every line in the resource documents, yet highlight the suits?

My existing dreadful hack to complete this (at the very least on files that do not have 10000+ successive lines without suits) is:

$ grep -B 9999 -A 9999 test test

If grep can not complete this, exists an additional command-line device that supplies the very same capability? I've adjusted ack, yet it does not appear to have an alternative for it either.

0
2019-05-07 02:00:42
Source Share
Answers: 3

You can attempt:

perl -MTERM::ANSIColor -nle '/pattern/ ? print colored($_, 'color') : print' test

Not really mobile nonetheless, and also also if Perl is mounted, you might require to download and install an additional component. On top of that it will certainly tint the whole line, not simply the search word.

0
2019-05-09 00:19:57
Source
grep --color -E "test|$" yourfile

What we're doing below is matching versus the $ pattern and also the examination pattern, clearly $ does not have anything to colourize so just the examination pattern obtains shade. The -E simply activates expanded regex matching.

You can create a function from it conveniently similar to this :

highlight () { grep --color -E "$1|$" "${@:1}" ; }
0
2019-05-08 22:39:46
Source
ack --passthru --color string file

for Ubuntu and also Debian, make use of ack - grep as opposed to ack

ack-grep --passthru --color string file
0
2019-05-08 19:12:35
Source