Hi folks,

On Wednesday 19 Feb 2003 9:15 am, Lance wrote:
> 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`;

Quite often I see commands like this, and I have to wonder why?

The above command forks a process and runs 'cat'.  It then forks another 
process 'lc' and pipes the output from cat to the input of lc.  This seems a 
lot of overhead when you can simply supply lc with the file name to read.

BTW, lc doesn't exist on my box, so I'd have to use 'wc -l' which I'm guessing 
'lc' was just an alias to anyway.   If you use

my $linecount=`wc -l file.txt`;

you'll give your machine less work to do.  Also, I don't know how well DOS 
systems handle pipes and multiple processes.  In fact you probably don't have 
'wc' on a DOS system either.  You'll probably find that your routine is as 
good as it gets anyway. 

Gary

>
> 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.

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to