On 4/19/05, Dave Adams wrote:
> I have a 2 gig log file and I need to read only the last 10 lines.
> 
> I was also thinking something like:
> 
> 1. Read in File
> 2. Get number of lines
> 3. Print last ten lines
> 
> or something like that.
> 

On a 2gig file, that is very inefficient. Much better I think to use Tie::File:
############# begin code
use strict;
use warnings;
use Tie::File;
my @array;
my $file = "filename";
tie @array, 'Tie::File', $file or die "Couldn't tie to '$file': $!\n";
for (reverse(1..3)) {
   print $array[-$_] . "\n";
}
############# end code
It is much faster because it doesn't read the entire file into memory,
or go over all of it just to get to the last 10 lines. Just be careful
- Tie::File lets you also modify the file, if you want. Read:
http://perldoc.perl.org/Tie/File.html
for more details.

HTH,
-- 
Offer Kaye

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to