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:
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 ~/.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:
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:
Exporting this variable:
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:
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
- Reasons why 2015 will be year for Linux on the desktop
- Manage and read your ebooks on Linux with Calibre
- The Best Linux-Based Home Automation Systems for Under $300
Find me on Google+