16.6 C
Wednesday, December 1, 2021

How to see grep output in color with highlighting feature

Must read

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.Now you know grep can color-highlighting the matched text or words in its output. However, by default, that option is turned off. So let us see how to colorized grep by default for viewing the entire file with highlighted matches on Linux or Unix-like systems.

grep with color output syntax

The syntax is as follows:

grep --color 'string' /path/to/file
fgrep --color foo file
egrep --color 'foo|bar' input
grep --color='auto' [options] patterns file

For example, search for a word called vivek in /etc/passwd using colorized grep as follows:
grep --color="auto" vivek /etc/passwd
Surround the matched text as follows:

vivek:x:1000:1000:Vivek Gite:/home/vivek:/usr/bin/bash

Finding string with grep with color highlighting turned on

Pass the --color option to the grep command as follows:
$ grep --color="auto" -i error /var/log/messages

Oct  9 16:12:14 vivek-desktop kernel: [   11.555442] bt878: probe of 0000:05:00.1 failed with error -22
Oct 10 17:35:28 vivek-desktop kernel: [   10.564710] bt878: probe of 0000:05:00.1 failed with error -22
Oct 11 10:15:34 vivek-desktop kernel: [   12.187477] bt878: probe of 0000:05:00.1 failed with error -22
Oct 11 14:29:56 vivek-desktop kernel: [   11.135309] bt878: probe of 0000:05:00.1 failed with error -22

Now all matched text displayed using red color. The --color option to matches in the input in red color by default. Color is added via ANSI escape sequences. To change color use environment variable GREP_COLOR. Following will set background to red and foreground to white:
$ export GREP_COLOR='1;37;41'
$ egrep --color=auto -i '(error|fatal|warn|drop)' /var/log/messages

I recommend putting the following in your ~/.bash_profile ~/.bashrc file:
$ vi ~/.bash_profile
Append the following alias:
export GREP_COLOR='1;37;41'
alias grep='grep --color=auto'

Save and close the file. Please note that --color option works with many GNU text utilities, so feel free to use the same.

Colorized grep for viewing the entire file with highlighted matches

There are three options that we can pass to the --color[=WHEN] or --colour[=WHEN]. WHEN can be:

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

  1. --color=never : Turn off coloruing
  2. --color=always : Always try to display matched string/words in color on the terminal
  3. --color=auto : Automatic stuff

Easily find strings with grep color highlighting feature all the time

You need to set up or create a permanent Bash alias on Linux/Unix as follows in your ~/.bashrc:

alias grep='grep --color=always'
alias egrep='egrep --color=always'
alias fgrep='fgrep --color=always'
# optional 
alias xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'

How to see grep output in color with highlighting feature with other commands

Here is one example where --color=always causes “raw” control characters to be displayed using the less command:

grep --color=always foo /path/to/input | less -r
grep --color=always dns /etc/passwd | less -r
# the following will not work as we failed to pass the always option
grep --color dns /etc/passwd | less -r

It will also work with the more command:
grep --color dns /etc/passwd | more

Summing up

The --color option surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority. See the grep command man page by tying the following man command:
man grep
man egrep


Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

- Advertisement -

Latest article