Highlighting Word Searched in Text Files

By: Jim Limmer

January 12, 2006 12:00 am





Applies to:

  • SUSE Linux Enterprise Server 9
  • SUSE Linux Professional 9.2-9.3
  • SUSE Linux 10.0
  • Novell Linux Desktop 9

PROBLEM: Often times while administering your servers, you’ll need to go through pages of text logs looking for a certain word, IP address, or other text. Sure, you could use the long time favorite, grep, but that only will present you with the line containing the data you need. Sometimes you will need to see the surrounding lines as well.

SOLUTION: For me, the solution was to be able to colorize an IP address or username (or any other text) from my log file (or any other text file). The solution is quite simple.

Copy this one-line script into an empty text file, somewhere in your path:

    perl -p -e ‘s/’$1’/\e[31m’$1’\e[0m/’

I save it as “highlight” but of course you are free to name it whatever you like.

EXAMPLE: Use it like this at a command prompt:

    cat filename | highlight SEARCHWORD

For example, if I wanted to see instances of the word “kernel” in my system log, I’d type:

    cat /var/log/messages | highlight kernel

Give it a try!

0 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 5 (0 votes, average: 0.00 out of 5)
You need to be a registered member to rate this post.

Categories: Uncategorized

Disclaimer: As with everything else in the SUSE Blog, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.