[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] How to simulate a call to 'tail' in MacPerl



At 15.38 -0500 1999.01.29, John Gilmore-Baldwin wrote:

>
>You could use the seek function in perl to get toward the end of a file
>(after doing a file size check), the read the file line by line and print
>out the last line you encounter.
>


Based on the comments I received, I came up with the following script.
It seems rather efficient. Would anyone like to propose ways to improve
this script or comment on its performance?

Stephane.


____Beginning of script_____

#! /usr/bin/perl

$myFile = 'HD Interne 8GB:files:somefile.log';

$lastline = &tail($myFile);

print $lastline;


sub tail {

  my($log)=@_;
  my(@line,$i,$filelength,$char)='';

  open (LOG, $log) || die $!;
  seek (LOG, -2, 2);      # positionne le fichier 2 octets avant
                          # le caract¸re de fin de fichier (EOF)

  $filelength=tell(LOG);
  # sleep(1);
  # seek(LOG,0,1);

  for ( $i=$filelength;$i >= 0; $i--)
  {
    seek(LOG,$i,0);
    read(LOG,$char,1);
    if ($char ne "\n") {
      push(@line,$char);
    } else {
      last;
    }
  }

  return reverse(@line);
}

____End of script_____






               ____S__t__e__p__h__a__n__e______J__o__s__e____

                     mailto:stephane.jose@sympatico.ca


                 Don't you ever try to look behind my eyes
                 You might not like what they have seen...

                 Ron L. Hoover - Joe's Garage

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch