Coder Perfect

Like grep, highlight text but don’t filter out [duplicate] text.

Problem

When you use grep, any text in a line that matches your regular expression will be highlighted.

What if I want this behavior but also want grep to print all lines? After a short search of the grep man page, I came up empty-handed.

Asked by Martin Konecny

Solution #1

Use the ack command. Here’s how to use the —passthru option: ack. It also has the advantage of supporting complete Perl regular expressions.

$ ack --passthru 'pattern1' file_name

$ command_here | ack --passthru 'pattern1'

You may also use grep to accomplish this:

$ grep --color -E '^|pattern1|pattern2' file_name

$ command_here | grep --color -E '^|pattern1|pattern2'

This will make all lines match and draw attention to the patterns. Because it is not a character, the will not be printed or highlighted at the beginning of each line.

(It’s worth noting that most configurations will use —color by default.) It’s possible you won’t need that flag).

Answered by holygeek

Solution #2

You can double-check that all lines match, but there’s little use in highlighting matches that aren’t relevant.

egrep --color 'apple|' test.txt 

Notes:

Answered by Sorin

Solution #3

EDIT:

This works with the grep command in OS X Mountain Lion:

grep --color -E 'pattern1|pattern2|$'

This is preferable to ‘|pattern1|pattern2’ because the $ matches at the end of the line, whereas the part of the alternation matches at the beginning. Because pattern1 and pattern2 have already been matched and the engine is eager, some regular expression engines will not highlight them.

Because the regex engine recognizes the empty alternation at the end of the pattern string matches the beginning of the subject string, something similar happens with ‘pattern1|pattern2|’.

[1]: http://www.regular-expressions.info/engine.html

FIRST EDIT:

Perl was the solution I came up with:

perl -pe 's:pattern:\033[31;1m$&\033[30;0m:g'

This implies that you’re using an ANSI-compliant terminal.

ORIGINAL ANSWER:

If you’re stuck with an unusual grep, try this:

grep -E --color=always -A500 -B500 'pattern1|pattern2' | grep -v '^--'

To get all the lines you desire, change the numbers.

Even when the context of consecutive matches overlaps, the second grep simply removes extraneous — lines inserted by the BSD-style grep on Mac OS X Mountain Lion.

When context overlaps, I believed GNU grep omitted the — lines, but it’s been a while, so I could be wrong.

Answered by willkil

Solution #4

You can use https://github.com/kepkin/dev-shell-essentials to run my highlight script.

It’s better than grep because each match can be highlighted with its own color.

$ command_here | highlight green "input" | highlight red "output"

Answered by kepkin

Solution #5

Because you want matches highlighted, this is most likely for human consumption (rather than piping to another program), hence a good approach is to use:

less -p <your-pattern> <your-file>

And if you don’t care about case sensitivity:

less -i -p <your-pattern> <your-file>

This also has the benefit of having pages, which is useful when dealing with large amounts of data.

Answered by acros

Post is based on https://stackoverflow.com/questions/7393906/highlight-text-similar-to-grep-but-dont-filter-out-text