Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorized grep -- viewing the entire file with highlighted matches

I find grep's --color=always flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could.

I'd really like to cat a file and see the entire file with the pattern matches highlighted.

Is there some way I can tell grep to print every line being read regardless of whether there's a match? I know I could write a script to run grep on every line of a file, but I was curious whether this was possible with standard grep.

like image 449
zslayton Avatar asked Jun 11 '09 14:06

zslayton


People also ask

How do you grep with colors?

The grep command is the de facto tool for searching text files. However, when there are too many matches, finding the requested text in the search results can be difficult. So grep comes with --color='auto' option. It surrounds the matching string with the colour, thus resulting in enhanced output.

How do I change the color of grep?

You can cascade greps with different colors by specifying --color=always and using the regular expression 'foo|$' to pass all lines. To update the entire line, modifying the regex is the wrong approach. Instead, use GREP_COLORS='sl=1;31' grep ... and specify sl to color the whole line.

How do you highlight a word in Unix?

For instance, in Konsole, start Find (ctrl+shift+F) and type your word. The word will then be highlighted whenever it occurs in new or existing output until you cancel the function.

How do you grep before and after lines?

You can use option -A (after) and -B (before) in your grep command.


2 Answers

Here are some ways to do it:

grep --color 'pattern\|$' file grep --color -E 'pattern|$' file egrep --color 'pattern|$' file 

The | symbol is the OR operator. Either escape it using \ or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the egrep command instead of grep.

The search text "pattern|$" is actually a trick, it will match lines that have pattern OR lines that have an end. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.

To also pass the colored parts through pipes, e.g. towards less, provide the always parameter to --color:

grep --color=always 'pattern\|$' file | less -r grep --color=always -E 'pattern|$' file | less -r egrep --color=always 'pattern|$' file | less -r 
like image 183
Ryan Oberoi Avatar answered Sep 28 '22 06:09

Ryan Oberoi


Here's something along the same lines. Chances are, you'll be using less anyway, so try this:

less -p pattern file 

It will highlight the pattern and jump to the first occurrence of it in the file.

You can jump to the next occurence with n and to the previous occurence with p. Quit with q.

like image 23
Dennis Williamson Avatar answered Sep 28 '22 04:09

Dennis Williamson