Nikola Janceski wrote:

> without using 'tail' how can I get the trailing 5 lines of a large file
> quickly, without loading it all to memory?
> 
> is there anyway without pop and shifting through an array foreach line?
> (<= this is the only way I could think of doing it)
> 

with a little seek and tell, try:

#!/usr/bin/perl -w
use strict;

my @character;
my @lines;
open(FH,"foo.txt") || die $!;
seek(FH,(-s "foo.txt")-2,0);
while(1){

        read(FH,$b,1);

        if($b eq "\n"){
                unshift(@lines,join('',@character) . "\n");
                @character = ();
                last if(@lines == 5);
        }else{
                unshift @character,$b;
        }

        seek(FH,tell(FH)-2,0);
        next unless(tell(FH) == 1);

        read(FH,$b,1);
        unshift @character,$b;

        seek(FH,0,0);
        read(FH,$b,1);
        unshift @character,$b;

        print join('',@character),"\n";
        last;
}
close(FH);

#-- last 5 lines
print join('',@lines);

__END__

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to