How to colorize the output of grep

Linux logo

Grep is a very useful Linux command. In short grep searches an input stream for a pattern and displays the results. In this guide I will show you how to colorize the output of grep by highlighting the pattern you were searching for.
Supposing that you are using the bash shell open ~/.bashrc with your favorite text editor:
gedit ~/.bashrc
and paste the following line:
alias grep="grep --color=auto"
Now open a new terminal (or source ~/.bashrc) and try the grep command. This is how the default and colorized grep look like:
Colorized grep command
Of course you can change those colors. In ~/.bashrc to change the foreground color paste the following line:
export GREP_COLOR="1;37"
or to change bot foreground and background
export GREP_COLOR="1;37;42"
Here is the output of grep.
Colorized grep command
The first number indicates that we want the font to be bold. The second one is the foreground (the text's) color, and the third one is the background color. Here is a list with the color numbers of the shell.
Color Foreground Background
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47

0 comments:

Post a Comment

www.comhttp.blogspot.in. Powered by Blogger.