0

This may not be the best way to do the below, so any comments are appreciated.

I'm currently tailing a number of log files and outputting them to screen, so that I get a quick overview of the system.

What I would like to do is to highlight different messages [INFO], [WARN] and [ERROR]

The following syntax works fine on the command line, but fails when being called from Perl

system ("tail -n 5 $ArchiverLog | awk '{if ($4 ~ /DEBUG/)print "\033[1;33m"$0"\033[0m"; else if ($6 ~ /ERROR/) print "\033[1;31m"$0"\033[0m"; else print $0}'");

I believe Perl can do this

Should I read in the file line by line, match on the words and print to screen (I only want the last 10 lines). Is that a better option?

I've also seen reference to a2p, which is an awk to Perl translator. Would that be people's preferred choice?

4
  • You should take a look at the File::Tail module. Or Tail::Tool is more advanced and will allow you to tail multiple files simultaneously Commented Jun 24, 2014 at 11:47
  • Please show the rest of your Perl code so that we can see what you need in context. And a sample of your log file would be nice Commented Jun 24, 2014 at 11:49
  • Not got round to learning modules yet and how they work.. Commented Jun 24, 2014 at 13:11
  • The perhaps you should take a look? Every module is different and has its own documentation on CPAN that you must read before using it. But most modules will either provide new functions (subroutines) that you can use, or will offer an object-oriented interface Commented Jun 24, 2014 at 13:31

1 Answer 1

1

It seems crazy to use one powerful scripting language to call up another one so it can do something which the first one can do very well, so I would not persist with trying to call up awk from perl.

I have not had much experience with a2p, rather I tend to just translate such snippets by hand.

#!/usr/bin/perl
use strict;
foreach(`tail -n 5 $ArchiverLog`) {
        my @f = split;
        if ($f[4] =~ /DEBUG/) {
                print "\033[1;33m$_\033[0m";
        } elsif ($f[6] =~ /ERROR/) {
                print "\033[1;31m$_\033[0m";
        }  else {
                print $_;
        }
}

(Hard to say if the above is completely correct without some sample input to test it with).

As Borodin says in the comments a more elegant solution would be to use a Tailing module from CPAN rather than calling up tail as a subprocess. But for a quick tool that might be overkill.

NB: if $ArchiverLog comes from anywhere you don't have control of, remember to sanitise it, otherwise you are creating a nice security hole.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.