Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Printing the last few lines of the Error log

by Baz (Friar)
on Nov 20, 2001 at 00:15 UTC ( #126367=perlquestion: print w/replies, xml ) Need Help??

Baz has asked for the wisdom of the Perl Monks concerning the following question:

I was just wondering if any1 has a perl script that prints out the last few lines on a file; perhaps one that takes an arguement representing the number of lines to display ; )
  • Comment on Printing the last few lines of the Error log

Replies are listed 'Best First'.
(jeffa) Re: Printing the last few lines of the Error log
by jeffa (Bishop) on Nov 20, 2001 at 00:18 UTC
    I have always been partial to File::ReadBackwards.

    UPDATE:
    If you are getting what i think you are getting at, the Unix 'tail' command is a must for watching debug statements in your web server error log. Use the -f option which ouputs the appended data. I usually keep a terminal open right next to my browser for such occasions.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    

      I agree that running tail on error logs is a useful development technique, but I would never do this on a live server.

      tail doesn't check its output, so a malicious visitor to your Web site could send dangerous escape codes to your terminal. xterm, for example, has various features/vulnerabilities that are based on escape codes. This assumes that someone wants to attack your Web server and either knows or guesses that you are running tail on your error log, but it's always worth protecting against untrusted data on the Internet.

        I just want to make the observation that this warning about the side effects of tail reflects the amazing depth and quality of answers that I have come to expect from PerlMonks. I'm impressed that someone would have provided such an insightful response to a basic question.

        Dave Aiello
        Chatham Township Data Corporation

Re: Printing the last few lines of the Error log
by Fletch (Bishop) on Nov 20, 2001 at 00:22 UTC

    Aside from the obvious "Read man tail" reply

    #!/usr/bin/perl my $lines = shift || 10; my @buf; while( <> ) { push @buf, $_; shift @buf if @buf > $lines } print @buf; exit 0;
Re: Printing the last few lines of the Error log
by belg4mit (Prior) on Nov 20, 2001 at 01:50 UTC
    PPT to the rescue! tail

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

      I'm impressed!
      It works really good on my w2k...
      I don't know how I can have missed this great piece of code...

      It's a pain trying to get the last lines of a huge log-file
      on a M$-machine, but this will do the trick...

      /k2
        While not to disparage the PPT, you might want to have a look at the cygwin toolset for W32. It contains tail amongst many other unix tools, usable from both cmd and bash. Oh and under cmd.exe AFAIK you have no worries about using it as per earlier comments on security vulnerabilities.

        Yves / DeMerphq
        --
        Have you registered your Name Space?

(shockme) Re: Printing the last few lines of the Error log
by shockme (Chaplain) on Nov 20, 2001 at 00:28 UTC
    I'm sure there are better ways, but the following seems to work:
    #!/usr/bin/perl -w use strict; use Getopt::Long; my $line; my $NUM; my $FILE; GetOptions("f=s" => \$NUM, # -f=/var/log/messages "n=s" => \$FILE); # -n=30 if (!($NUM)) { $NUM = 20; } my @TailFile = `tail -$NUM $FILE`; foreach $line (@TailFile) { chomp $line; print "$line\n"; }

    Update: My comments were flip-flopped. Corrected.

    If things get any worse, I'll have to ask you to stop helping me.

Re: Printing the last few lines of the Error log
by fuzzysteve (Beadle) on Nov 21, 2001 at 05:25 UTC
    Obvious one with tail being mentioned: File::Tail
    not too much use on its own, but useful if you want to do stuff with the output.
    also mtail for those people that like a colorized output. (in python, but hey, nothing's perfect ;) )
Re: Printing the last few lines of the Error log
by Stegalex (Chaplain) on Nov 20, 2001 at 05:44 UTC
    Gimme some tail! Wee doggie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://126367]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-10-02 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (8 votes). Check out past polls.

    Notices?