> -----Original Message-----
> From: Madhu Reddy [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, February 19, 2003 1:25 PM
> To: [EMAIL PROTECTED]
> Subject: How to get 1st line, last line and no of lines in a file
>
>
> Hi,
> How to get first line, last line and no of lines in
> a file.....
>
> is there any perl functions available for that ?
> right now what i am doing is
>
> open file
> while (<FH>
> {
> $lines++;
> }
> close(FH)
>
> This operation is expensive..
> suppose, if file have millions of records,
> it will take more time....
>
> I think there should be some functions to get those..
> i appreciate u r help....
>
> Thanx in advance
> -Madhu
>
perldoc -q "number of lines in a file"
Found in E:\Perl\lib\pod\perlfaq5.pod
How do I count the number of lines in a file?
One fairly efficient way is to count newlines in the file. The
following program uses a feature of tr///, as documented in the
perlop manpage. If your text file doesn't end with a newline,
then it's not really a proper text file, so this may report one
fewer line than you expect.
$lines = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;
This assumes no funny games with newline translations.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]