Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

using perl to format tail -f

by danmcb (Monk)
on Apr 14, 2008 at 14:33 UTC ( #680300=perlquestion: print w/replies, xml ) Need Help??
danmcb has asked for the wisdom of the Perl Monks concerning the following question:

I have a log file which contains SOAP messages, and they're long. Very long. Each message ends up as a line of maybe 700 chars or so.

If this was an apache log, I would of course just do tail -f on it and use that to debug. But that won't work, it's too damn unreadable to be useful..

Of course, using perl to decode XML is a cinch. But How can I get perl to be a wrapper on tail -f, so that I can see formatted or filtered messages in real time, without all the crud?

Thank you kindly!

Replies are listed 'Best First'.
Re: using perl to format tail -f
by moritz (Cardinal) on Apr 14, 2008 at 14:46 UTC
    You can just pipe the output from tail -f to perl: tail -f /var/log/apache2/access.log | perl -pe 's/ /\t/g'

    This tail -f's the apache log file, and replaces each space with a tab (useless, but it shows how you can do it).

    And of course there's File::Tail on CPAN:

      Or to a script:
      #!/usr/bin/env perl use warnings; use strict; while(<>) { #... do something with the latest line, $_ }
      tail -f /var/log/apache2/access.log |
Re: using perl to format tail -f
by apl (Monsignor) on Apr 14, 2008 at 14:39 UTC
Re: using perl to format tail -f
by oko1 (Deacon) on Apr 14, 2008 at 15:46 UTC

    One way is to open 'tail -f' as a 'read-from' filehandle within your script, as documented in perlopentut.

    open Tail, '/usr/bin/tail -f /my/log/file |' or die "Pipe failed: $!\n +"; while (<Tail>){ # Do stuff }
    Human history becomes more and more a race between education and catastrophe. -- HG Wells

      IIRC, on some platforms, "tail -f" may buffer the output so that you will get batches of output rather than real-time output.

      This is only on some platforms, and other versions will have a flag to disable buffering.

      Just a tidbit that may effect your results.


Re: using perl to format tail -f
by poolpi (Hermit) on Apr 15, 2008 at 08:38 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://680300]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-10-18 16:40 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (103 votes). Check out past polls.