Nov 252012
 

Terminal
In the past I’ve posted an article regarding the basic usage of grep, one of the commands that I use daily at work to search a text within multiple files, or a text string in the list of running process of a Linux server, but I’ve forgot to show you a small and useful option : How to colour the output that you get from grep to highlight the text you were searching for in a long line.

This is a small option of this powerful command but can save your eyes when searching for a particular property of a Java process that is 4 or 5 lines long.

To be honest I’ve realised this thanks to an article of Alessio, posted recently on his nice blog: http://dark-linux.net and so the following is the translation by me of his article in Italian with some small addition in examples and explanations.




The grep command is one of the most frequently used commands in a Linux environment. With this command you can search within files words or strings. After seeing how to use it through my Guide to the Grep command, today I want to show you how to make this command more functional for our eyes, coloring with a color of your choice, the strings that you are looking for within files.

This is an example of a search with grepwith a coloured output:

grep

To get this result open a terminal and use your favourite editor to change the file .bashrc in your home directoy, in this example I’ll use vim

$ vim ~/.bashrc

And add these 2 lines:

alias grep="grep --color=auto"
export GREP_COLORS='0;31'

Save and try to use the grep command. The result should be the same of the former screenshot and the following:

Let’s see how to change the colors. The colors are defined in the string:

GREP_COLORS='0;31'

The value “31” stands for red font as defined by the color table 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

If we would like to see the text color in green, the value that we should use will be “32”.
Let’s see an example with the green color as background:

grep3

Exporting this variable:

export GREP_COLORS='0;30;42'

where “30” is used to define the color of the foreground, in this case black, and “42” is the background, the color green.

And if you are on a different computer and you want to just have 1 colored output without changing the .bashrc file ?
No problem you can use grep with the option --color=auto to get the output with the default color that usually is a red foreground:

grep4

This 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. WHEN is never, always, or auto.

To see the various combinations of Foregrounds and Backgrounds just refer to THIS page.

And now have fun in testing the different combinations and be faster in your searches with grep.

Popular Posts:

Flattr this!

  6 Responses to “Coloring Grep to easier research”

  1. Great tutorial. I found adding color to grep useful. Thanks!

  2. What does the value 0 stand for in export grep_colors=’0;31′

  3. is GREP_COLOR not GREP_COLORS

  4. I’ve recently built a tool that allows you to build up styles for GREP_COLORS. It’s available at https://dom.hastin.gs/files/grep-colors/

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*