Solved

how to highlight certain words while tailing log on unix/linux

Posted on 2012-03-25
10
746 Views
Last Modified: 2012-07-12
Hi,

Is there a way to highlight few words while tailing (tail -f logfile.log) log files on unix or linux.

Bascially, would like to highlight (ONLY the word or the word background color) for certain keywords ex "ERROR" or "FAILED" ...so while tailing (scrolls up) it can be easily noticed.

Right now, I have a script which scans the log file and write only the lines which I'm interested in to a different file...then I open the newly created file and so on.

thanks in advance
0
Comment
Question by:enthuguy
10 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 37763873
require Term::Cap;
my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
my $so = $term->Tputs('so');
my $se = $term->Tputs('se');
open T,'tail -f logfile.log|' or die $!;
$|=1;
while( <T> ){
    s/(ERROR|FAILED)/$so$1$se/g;
    print;
}
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 37765093
The less command can do what you want (did you know it can act like tail -f?)
less your log file (that is being written to). After a screen of output, you get a prompt.
Enter your search criteria, e.g. /ERROR|FAILED (less search strings are regular expressions).
Eventually you get another prompt (after ERROR or FAILED is displayed, highlighted, on line 1).
At this prompt, enter F (just a capital F). less will tail the file, and you will see highlighted matches as they occur. You can enter Control-C at any time to stop tailing; at which point you can use N (capital N) to visit previous matches.
If you want the match to show some way down the page so you can see preceding context, enter -jnumber at any prompt to set the line number where the matches are displayed.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 37765121
grep --line-buffered can also be you friend. E.g. tail -f logfile.log | grep --line-buffered -E -v stuff you don't want to see | less
Drive less as per previous post
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:enthuguy
ID: 37765488
thanks all

Getting exicted now...will try this tomorrow and let you know :)

thanks again
0
 

Author Comment

by:enthuguy
ID: 37765497
Hi ozo, do I need an package to be installed first ?  please let me know
0
 

Author Comment

by:enthuguy
ID: 38161896
I've requested that this question be closed as follows:

Accepted answer: 0 points for enthuguy's comment #37765497

for the following reason:

Not received additional info
0
 
LVL 29

Expert Comment

by:serialband
ID: 38160403
There's mtail, which adds ansi coloring to log file terms and can be modified to produce exactly what you need.

http://matt.immute.net/src/mtail/
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 38161897
@enthuguy: what extra information were you waiting for? Ozo posted a Perl script - you will have Perl on your system. I tried a variant of his script and it worked fine
09:31:26$ cat ee90.pl
#!/usr/bin/perl
require Term::Cap;
my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
my $so = $term->Tputs('so');
my $se = $term->Tputs('se');
open T,'tail --follow=name /var/log/debug|' or die $!;
$|=1;
while( <T> ){
    s/(MARK|error)/$so$1$se/g;
    print;
}

Open in new window

Substitute your file for /var/log/debug and your words of interest for MARK|error (any number of words, separated by bar characters)
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 38165061
I suggest split points between ozo http:#a37763873 and duncan_roe http:#a38161897. Ozo's post defined the basic mechanism but without sufficient instructions for enthuguy to understand how to use it - duncan_roe provided these instructions.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question