Solved

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

Posted on 2012-03-25
10
715 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
 

Author Comment

by:enthuguy
ID: 37765488
thanks all

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

thanks again
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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 27

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now