Mark Richmond wrote:

> 
> Hi:
> 
> I have may huge log files where all I care about is the error at the end
> how can seek backwards to find my pattern and snip off the end.
> I'm looking for strings like. "======== Rebuilding "link" ========"  which
> only occur once when reading backwards but may times when reading forward.

here is one way of doing it with seek/tell. it reads the foo.txt file line 
by line from the end:

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

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

        read(FH,$b,1);

        if($b eq "\n"){
                #-- one line. do your stuff here
                print join('',@character),"\n";
                @character = ();
        }else{
                unshift @character,$b;
        }

        #-- the following is for the first 2 lines from the top
        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);

__END__

i am sure you can make the above better. :-)

david

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

Reply via email to