Hi Eelco,

This solution requires the File::Tail module available from CPAN
(File::Tail, in turn, requires Time::HiRes):

use File::Tail;

# File to read from
my $text_file = '/www/logs/error_log';

# Number of lines to read from end of file
my $num_lines = 25;

# Create a new File::Tail object starting at
# the $num_lines'th line from the end of the file
my $file = new File::Tail(
  name => $text_file,
  tail => $num_lines
);

# Iterate over each line of the file, translate,
# print, and increment a counter that keeps track
# of the number of lines we've output
my $counter = 0;
while($_ = $file->read) {
  s#<#&lt;#g;
  s#>#&gt;#g;

  print;

  last if ++$counter == $num_lines;
}

Note that it's necessary to keep track of the number of lines you've output
because of the behavior of $file->read. From the File::Tail manpage:

 "read" returns one line from the input file. If there
 are no lines ready, it blocks until there are.

This effectively means that your while-loop will never exit unless you
explicitly tell it so.

Best regards,
David

At 1/24/2003 3:50 PM, Eelco Alosery wrote:

> I want to read my error_log and print out only the last 25 lines.
> 
> If I use this script:
> 
> $tekst_file = "/www/logs/error_log";
> open (BESTAND, "$tekst_file");
> my @temp = <BESTAND>;
> close(BESTAND);
> 
> @lines = reverse(@temp);
> 
> $start = 0;
> $end = 24;
> 
> for(my $a = $start; $a <= $end; $a++)
> {
> $lines[$a] =~ s/</&lt\;/g;
> $lines[$a] =~ s/>/&gt\;/g;
> 
> print <<ENDOFTEXT;
> 
> $lines[$a]<BR>
> ENDOFTEXT
> }
> 
> I see nothing, is it becouse the file is to large, its about 58600
> lines large.
> If I use this script on a small file it works perfect.
> 
> Any idea's are very welkom.
> 
> 
> Met vriendelijke groet,
> 
> Multi-Graphics
> Eelco Alosery
> Koekoeksbloem 11
> 8255 KH  Swifterbant
> Tel : 0321-380014
> Fax : 0321-843340
> [EMAIL PROTECTED]
> www.multi-graphics.nl
> www.hostingspot.nl

Reply via email to