Grep in Color

Posted on September 2nd, 2007 in Tech Tips by gmendoza

If you would like to make it easier to visually spot what you are looking for when using “grep”, try out the “–color” flag. This option highlights any matches in the output of your search, giving you an upper hand when trying to visually scan through complicated or cumbersome sequences.

For example, if you were looking for any IP address in your /etc/hosts.deny file that has the number “209″ in it, issue the following command:

$ grep --color 209 /etc/hosts.deny

Here is the comparison of the same output, with and without the color option.

Grep with no color Grep with color

As you can see, one might find it very easy to miss the fact that there are some IP addresses that have multiple octets with the value of 209. The color flag really comes in handy.

2 Responses to 'Grep in Color'

Subscribe to comments with RSS or TrackBack to 'Grep in Color'.

  1. blacky said,

    on December 28th, 2007 at 3:46 am

    I recommend this:

    alias grep=’grep –colour=auto’

    (for bash, that is) - This way, grep autodetects if its output is going to a terminal and then turns color on. If it’s inside a pipe (where the colors would wreak havoc), it doesn’t. Pretty much like ls’s –color=auto option.

  2. gmendoza said,

    on December 28th, 2007 at 9:33 am

    Ahh… I like this approach. Thanks for the input!

Post a comment