Tuesday, March 10, 2009

Highlight match with color in grep command

Like bash ls command, grep supports color in its output. i.e. you can highlight the text that grep matches with color.

This is controlled by "--color" option with grep command which basically surround the matching string with the marker find in GREP_COLOR environment variable.

$ grep --color=auto <pattern> <file>

Grep's default highlight color is red (shown below in the snap).

You can change this color by setting the GREP_COLOR environment variable to different combinations (from the color code list given below).

I use

$ export GREP_COLOR='1;30;43'

which basically highlights the matched pattern with foreground color black and background color yellow (shown below in the snap).

The set display attributes list:

0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden

Foreground Colours
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background Colours
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

Related post:
- Bash ls command color support
- Colorful man pages in ubuntu

1 comment:

© Jadu Saikia www.UNIXCL.com