Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Perl equivilant to tail -f

by jklcrash (Initiate)
on Mar 07, 2006 at 19:53 UTC ( [id://535015]=perlquestion: print w/replies, xml ) Need Help??

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

I am currently logging an xterm session that runs the following command
tail -f "filename" | grep "foo"
I orginially tried
tail -f "filename" | grep "goo" >>./file.txt
but there is a buffer issue it doesn't start writing until roughly 800 entries are present in the file im tailing. I am just curious if any one has a perl module that is equivialent to tail -f

Replies are listed 'Best First'.
Re: Perl equivilant to tail -f
by runrig (Abbot) on Mar 07, 2006 at 20:00 UTC
      thank you very much. FILE::TAIL works like a champ
Re: Perl equivilant to tail -f
by davidrw (Prior) on Mar 07, 2006 at 20:25 UTC
    I orginially tried tail -f "filename" | grep "goo" >>./file.txt but there is a buffer issue
    Just need an extra switch on the grep:
    tail -f "filename" | grep --line-buffered "goo" >>./file.txt
    (note that the grep manpage may incorrectly may '--line-buffering' -- details here)
Re: Perl equivilant to tail -f
by ikegami (Patriarch) on Mar 07, 2006 at 20:10 UTC

    File::ReadBackwards

    use File::ReadBackwards; my $file_name = "filename"; my $fh = File::ReadBackwards->new($file_name) or die("Unable to read log file $file_name: $!"); my @lines = reverse grep /foo/, grep defined, map { scalar $fh->readline() } 1..10;

    Update: Oops, I wrongly assumed -f worked the same way as in tar. My snippet emulates
    tail "filename" | grep "foo"
    rather than the desired
    tail -f "filename" | grep "foo"
    Disregard this post.

Re: Perl equivilant to tail -f
by smokemachine (Hermit) on Mar 07, 2006 at 23:25 UTC
    can be this?
    perl -e 'open FILE, "<".shift; @arq=<FILE>; $a=shift; foreach(@arq[-10 +..-1]){print if /$a/} while(1){while(<FILE>){print if /$a/}}' file re +gex

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-20 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found