On a Unix system you could use 'lc' to count the lines and 'top' or 'tail'
to read the first or last lines.  My Unix is getting rusty, but there are
functions to do what you want - so you could do something like:

my $linecount = `cat file.txt| lc`;

to get the line count. I'm sure that the lc command needs something else, so
you will have to play with it to get it to work.  I used to use something
like this in ksh to do line counts on lines with millions of lines, and it
would return pretty quick - but that was on some pretty impressive
hardware...

dunno what to do in the DOS world, other than the 'expensive' file
processing.

PS, I really need to get back into 'nix.  I can't believe I have forgotten
such *simple* stuff... ugh.


"Toby Stuart" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
>
> > -----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]

Reply via email to