On Jun 22, Kipp, James said:
>Anyone have any good routines they use to mimic the head and tail the UNIX
>head and tail utils ? I am writing a script for Win32 that I need to head
>and tail a large data text file. Any help is appreciated.
MBNA, eh? (japhy checks the credit card bills he receives...) ;) I guess
it'd be in my best interest (no pun intended) to help you, then.
Uri Guttman wrote File::ReadBackwards, available on CPAN, that can help
you emulate tail(1).
To emulate head(1), just read the first X lines of the file you want.
DO NOT PUT THE FILE INTO AN ARRAY. That is evil, as far as memory usage
and efficiency go. (So it's evil, in general.)
use File::ReadBackwards;
open HEAD, "< $file" or die "can't read $file: $!";
for (1 .. 5) {
push @head, scalar <HEAD>; # first 5 lines
}
close HEAD;
tie *TAIL, 'File::ReadBackwards', $file or die "can't read $file: $!";
for (1 .. 5) {
unshift @tail, scalar <TAIL>; # last 5 lines
}
untie *TAIL;
Notice how I unshift()ed to @tail -- this is so that the last 5 lines are
stored in normal order in the array.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **