[MacPerl-AnyPerl] Reading error_log

2003-01-28 Thread Tim Grant
Eelco,

Since your script reads the whole file into an array, that will certainly
cause trouble if the file is big enough. (Because you'll run out of memory.)

The script below should work on any size file -- just tested it on a 500 Mb
data file. Maybe it's not elegant, but it seems fast enough.

Regards,
-Tim Grant

$tekst_file = "/www/logs/error_log";

open (IN, "$tekst_file") or die "Can't open input file: $!\n";

my @lines;
my $linesWanted = 25;

while () {

$. >= $linesWanted and last;
push(@lines,$_);
}

while () {

push(@lines,$_);
shift(@lines);
}

close(IN);

foreach (@lines) {

s//>\;/g;
}

$" = "\n\n";
print "@lines";
exit;




Re: [MacPerl-AnyPerl] reading error_log

2003-01-28 Thread David Iberri
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#<#<#g;
  s#>#>#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 = ;
> close(BESTAND);
> 
> @lines = reverse(@temp);
> 
> $start = 0;
> $end = 24;
> 
> for(my $a = $start; $a <= $end; $a++)
> {
> $lines[$a] =~ s/ $lines[$a] =~ s/>/>\;/g;
> 
> print < 
> $lines[$a]
> 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