grep -e :searching multiple terms

By Eric Downing. Filed in Software, Utilities  |  
TOP del.icio.us digg

When you have to process log files, it can be very useful to highlight the terms you are seeking. One way to highlight a term is to use grep with the –color option. The drawback to using this method is that if you are chaining the grep command after another command then the last grep is all that would be highlighted.
I found that you could use the following to highlist all terms:

grep --color -e 'search|search2' 

So to break down the command line, first we have the grep command followed by the –color option which (if your terminal allows it) will highlight the search term within the screen results. This means that if you put this into a file, then the color highlighting will be lost. The ‘-e’ option states that the next argument is going to be a regular expression based argument.
The full syntax of regular expressions is beyond the scope of this article but basically is a pattern to match strings. The reason I am using a -e option is to support the ‘|’ option that allows me to search for more than one term. The reason that the pipe character is escaped is that otherwise it would be considered as a character to search for instead of a separator. This also will color either term within the output. So I am searching for search or search2 anywhere within the files that I pass in.

Leave a Reply