It was coded to grab the last few bits of a gigantic logfile we use here and may not be suitable for your needs. Enjoy.my $g=lastn("/usr/dict/words", 400); print while(<$g>);
sub lastn { my($file, $lines)=@_; my $fh; $lines++; if (! open($fh, $file) ) { print "Can't open $file: $!<P/>"; return; } binmode($fh); sysseek($fh, 0, 2); # Seek to end my $nlcount=0; while($nlcount<$lines) { last unless sysseek($fh, -1, 1); sysread($fh, $_, 1, 0) || die; $nlcount++ if ( $_ eq "\n"); last if $nlcount==$lines; last unless (sysseek($fh, -1, 1)); } seek($fh, sysseek($fh, 0, 1), 0) || warn; $fh; }
|
---|
Replies are listed 'Best First'. | |
---|---|
EEK! sysread() is expensive!
by chip (Curate) on Dec 19, 2001 at 12:50 UTC | |
by SpongeBob (Novice) on Dec 22, 2001 at 01:52 UTC | |
by chip (Curate) on Dec 22, 2001 at 04:35 UTC | |
Re: Last N lines from file (tail)
by SpongeBob (Novice) on Dec 19, 2001 at 01:35 UTC | |
by clintp (Curate) on Dec 19, 2001 at 01:49 UTC | |
by SpongeBob (Novice) on Dec 22, 2001 at 02:02 UTC | |
Re: Last N lines from file (tail)
by belg4mit (Prior) on Dec 19, 2001 at 00:39 UTC | |
Re: Last N lines from file (tail)
by Juerd (Abbot) on Dec 19, 2001 at 00:00 UTC |