Novell is now a part of Micro Focus

Highlighting Word Searched in Text Files

Novell Cool Solutions: Trench
By Jim Limmer

Digg This - Slashdot This

Posted: 12 Jan 2006

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!

Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions.

© Copyright Micro Focus or one of its affiliates