Bohack

Check In and Tune Out!
 
 
« Keep Your Soldering Iron Tip Clean
Automated Installation Framework »

Linux Coloring of the Results



When reviewing a UNIX log file, I often feel like I’m looking for a needle in a haystack. Even after I ‘grep’ out what I’m looking for I still can’t find it. I also like to use the ‘tail –f’ command to watch a rolling log, but again the information scrolls too fast and you just can’t find what you’re looking for in the quick moving results. To find the interesting information we will obviously use the ‘grep’ command, but to identify the results in the information there are a couple of methods.

The ‘grep’ command will support ANSI colorization and this can be done a number of ways. The first way is to specify the –color or –colour option on the command line. The second way is to export a variable that ‘grep’ looks at when executed. You will most likey want to add the export lines to your .profile or .bashrc file.

cat {somelogfile} | grep  --color {matching text}

or

tail -f {somerollinglog} | grep --color {matching text}
export GREP_OPTIONS="--color=auto"
export GREP_COLOR='1;31'

Grep colorization is good, but sometimes you need to see all the lines from the log file and focus on just what you are looking for. In this case we need to take a different approach; grep will filter and show just the lines that contain the information matching the regular expression we’ve feed it. In order to see all the lines and only highlight the specific pieces we want to see, we will need to run the information thru a mechanism that will highlight just the matching piece. I originally came across this poorly written piece of PERL script that I adapted and refined over many iterations.

#!/usr/bin/perl
### Bohack 2006i
### Usage: hilite <ansi_command> <target_string>
### Purpose: Will read text from standard input and perform specified highlighting
### command before displaying text to standard output.
### License: GNU GPL
$|=1; # don't buffer i/o
$target = "$ARGV[0]";
$color = "\e[31m";
$end = "\e[0m";while(<STDIN>) {
s/($target)/$color$1$end/i;
print $_;
}

Remeber to chmod the file for execute permissions and put it in the path. (i.e. chmod 0777 hilite)

cat {somelogfile} | hilite {matching text}
or
tail -f {somerollinglog} | hilite {matching text}

Tags: Linux

This entry was posted on Sunday, November 2nd, 2008 at 12:22 pm and is filed under Linux / Unix.
You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.

Comments are closed.

  • Channels

    • Beer (2)
    • Blog (1)
    • Exchange (2)
    • Ham Radio (1)
    • Homebrew (3)
    • Linux / Unix (4)
    • Misc (1)
    • Mods (4)
    • Networking (1)
    • Programming (4)
    • Recipes (2)
    • Scripts (7)
    • Security (1)
    • Software (2)
    • Spam (1)
    • Telco (7)
    • Virtual PC (1)
    • VMware (3)
    • VOIP (3)
    • Windows (16)
    • Windows 2008 (4)
    • Windows 7 (5)
  • Archives

    • September 2012
    • April 2012
    • March 2012
    • February 2012
    • January 2012
    • November 2011
    • September 2011
    • July 2011
    • April 2011
    • February 2011
    • January 2011
    • October 2010
    • August 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • June 2009
    • May 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
  • Links

    • Blogarama Blogarama – The Blog Directory
    • BlogHub Blog Directory
    • Blogrankings Technology Blogs – Blog Rankings
    • Blogville.us blogville.us
    • Buzzerhut free directory | buzzerhut.com
    • Ontoplist Online Marketing
    • Primechoiceautoparts Discount Auto Parts
    • PTC My Employer
 

  Copyright - Bohack 2025 ©