Saturday, February 23, 2008

HowTo: Highlight Matching Grep Results

Here's a quick entry on how to have a highlighted and colored text from grep resulting strings and characters.

Assuming we have /var/log/maillog and we wish to grep it with multiple search strings. Well this would be faily simply

# cat /var/log/secure | grep 'Accept\|refuse'

What if we want to make resulting matching grep strings appear in colored text or highlighted text. Highlighted and colored characters or words appear much more highlighted and distinguished specially when you are viewing large number of text lines from a log file.

Here's how to set and kick the ball.

Say from root terminal, simply issue

# alias grep='grep --color=auto'

And try issuing the later command again like so

# cat /var/log/secure | grep 'Accept\|refuse'

Make permanent between reboots for user root.

# echo "alias grep='grep --color=auto'" >> /root/.bashrc

Could be useful on tailing and grepping log files when needed.

Quick and easy. Enjoy.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy