For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > Reading last 10 lines in file on Win32 Systems









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading last 10 lines in file on Win32 Systems
Dave Adams

2005-04-19, 3:56 pm

I am new to PERL but how would I write a script that would read in a
file and output the last 10 lines?

I guess it is sort of like the UNIX command of Tail.

Any ideas, much appreciated.

DA
Wagner, David --- Senior Programmer Analyst --- WG

2005-04-19, 3:56 pm

Dave Adams wrote:
> I am new to PERL but how would I write a script that would read in a
> file and output the last 10 lines?
>=20
> I guess it is sort of like the UNIX command of Tail.
>=20
> Any ideas, much appreciated.
>=20
> DA

I did a cpan search and got File::ReadBackwards . What type of files desr=
ing to read and size of said files?

Wags ;)


****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Dave Adams

2005-04-19, 8:55 pm

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.

DA

On 4/19/05, Wagner, David --- Senior Programmer Analyst --- WGO
<David.Wagner@freight.fedex.com> wrote:
> Dave Adams wrote:
> I did a cpan search and got File::ReadBackwards . What type of fi=

les desring to read and size of said files?
>=20
> Wags ;)
>=20
> ****************************************
***************
> This message contains information that is confidential
> and proprietary to FedEx Freight or its affiliates.
> It is intended only for the recipient named and for
> the express purpose(s) described therein.
> Any other use is prohibited.
> ****************************************
***************
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>

David --- Senior Programmer Analyst --- WGO Wagner

2005-04-19, 8:55 pm

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

You may want to think this out in that you want to do some type of s =
against an filehandle to get to the end of the data otherwise you will =
end up reading all 2 gigs. You might at least give a quick shot at =
File::Readbackwards to see what happens and time frames, etc.

Wags ;)[color=darkred]
> On 4/19/05, Wagner, David --- Senior Programmer Analyst --- WGO
> <David.Wagner@freight.fedex.com> wrote:

Offer Kaye

2005-04-19, 8:55 pm

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


On a 2gig file, that is very inefficient. Much better I think to use Tie::F=
ile:
############# begin code
use strict;
use warnings;
use Tie::File;
my @array;
my $file =3D "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,
--=20
Offer Kaye
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com