It is working
Thank you, Flemming Greve Skovengaard and Randy W. Sims

With kind regards,
Met vriendelijke groet,

Maurice Lucas
TAOS-IT
----- Original Message ----- 
From: "Flemming Greve Skovengaard" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Cc: "Maurice Lucas" <[EMAIL PROTECTED]>
Sent: Friday, July 30, 2004 1:41 PM
Subject: Re: howto 'cat file|grep "foo bar"|wc -l' in perl


> Maurice Lucas wrote:
> > Hello,
> >
> > I just started using perl and want to rewrite a simple bash script i've
been
> > using in the past to perl.
> >
> > I want to cat file|grep "foo bar"|wc -l and tried it the following way
which
> > worked for foobar as one word and not as two words.
> >
> > ---
> > #!/usr/bin/perl
> > $logfile = "/var/log/logfile";
> > $grep_cmd = "/bin/grep";
> > $string = $ARGV[0];
> > $count = 0;
> >
> > open(LOG, "cat $logfile|$grep_cmd $string|") || die "Ooops";
> > while($line = <LOG>) {
> >             $count++;
> > }
> >
> > print "$count\n";
> > ---
>
> I would write like this:
>
> #!/usr/bin/perl
>
> use strict;    # Always use strict
> use warnings;  # Very helpful, especially if you are new to Perl
>
> die "No argument\n" if ( @ARGV == 0 );
>
> my $logfile = "/var/log/logfile";
>
> my $string = $ARGV[0];
> my $count = 0;
>
> # Open a file like this
> open( LOG, $logfile ) or die "Cannot open '$logfile': $!\n";
>
> while( <LOG> ) {
> my $line = $_;    # $_ contains the current line of LOG
> ++$count if ( $line =~ m/$string/ );
> }
>
> print "$count\n";
>
> >
> > calling this program with
> > ./count.pl foobar works and with
>
> Yes, foobar is one argument.
>
> > ./count.pl foo bar doesn't works neither does
>
> No, because 'foo bar' is two arguments, arguments is separated by spaces.
>
> > ./count.pl "foo bar" works
>
> Yes, "foo bar" is one argument.
>
> >
> > How do I write this the right way?
>
> My way works, 'cause I'm always right :)
> No seriously TIMTOWTDI.
>
> >
> > With kind regards
> > Met vriendelijke groet,
> >
> > Maurice Lucas
> > TAOS-IT
> >
> >
>
>
> -- 
> Flemming Greve Skovengaard                     Man still has one belief,
> a.k.a Greven, TuxPower                         One decree that stands
alone
> <[EMAIL PROTECTED]>                    The laying down of arms
> 4112.38 BogoMIPS                               Is like cancer to their
bones
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to