2009-05-06

How to make grep highlight the keyword

By default, grep does not highlight the keyword or the search term. Making grep highlighting the search term can make the visibility of the keyword more obvious if it hide in between the words.


The parameter for grep to highlight the keyword is "--color".
#start of somefile

the big brown fox jumped over the lazy dog

the big brown dog jumped over the lazy fox

the brown fox jumped over the big lazy dog

#end of somefile

e.g.
grep --color -i dog somefile
would display :
the big brown fox jumped over the lazy dog

the big brown dog jumped over the lazy fox

the brown fox jumped over the big lazy dog

To make the highlight feature permanent, do an alias in .bash_profile or .bashrc :
alias grep='grep --color'

Additionally, to change the color of grep, set the term variable GREP_COLOR to the respective color by using "export".
e.g.
For red wordings, set :
export GREP_COLOR=31

To make the color permanent, put it into .bashrc or .bash_profile.


As for other colors, refer to the below escape sequence :
Standard Attributes
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

AdiĆ³s !!!

No comments: