Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: pure perl tail revisited

by Aristotle (Chancellor)
on Sep 10, 2002 at 16:15 UTC ( [id://196734]=note: print w/replies, xml ) Need Help??


in reply to pure perl tail revisited

The "linux only" comment is obsolete in your updated version. It can still do with some cleaning up. Note this will still fail if the last X lines of the file are larger than 2*200*X bytes, so you cannot compare it against the other solutions as it doesn't do the same thing.
#!/usr/bin/perl -w use strict; die "Usage: $0 file numlines\n" unless @ARGV == 2; my ($filename, $numlines) = @ARGV; # open or die first so that we don't stat a file we don't know exists open my $fh, "<", $filename or die "Couldn't open $filename: $!"; my $chunksize = 2 * 200 * $numlines; my $filesize = -s $fh; $chunksize = $filesize if $filesize < $chunksize; # why seek twice? seek $fh, -$chunksize, 2; my @tail = <$fh>; splice @tail, 0, @tail - $numlines; print @tail; __END__ $ perl -le'print "a"x500 for 1..10' > t.dat $ tailz t.dat 5 | wc -l 1
You need a loop.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: pure perl tail revisited
by zentara (Archbishop) on Sep 11, 2002 at 20:14 UTC
    Thanks for showing me that splice trick. I don't think it took anything off the benchmark times, but it removed the temp array I was using. When I do tailz t.dat 5 | wc -l I get 5. (Just lucky). perl -le'print "a"x5000 for 1..10' > t.dat does give bad results The code still depends on you knowing your max line length beforehand, but I think this is true for more logs and text files. I'm happy now, at least I figured out what Merlyn meant when he said there was a faster method than Tie::File or File::ReadBackwards.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196734]
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: (5)
As of 2024-04-16 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found