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";
^^^^^^^
When interpolated, $string can show up as multiple words. You need to quote it by surrounding it with single quotes, but...


while($line = <LOG>) {
            $count++;
}

print "$count\n";
---

calling this program with
./count.pl foobar works and with
./count.pl foo bar doesn't works neither does
./count.pl "foo bar" works

How do I write this the right way?

You can easily do all this in perl. You're nearly there in your example above. Just open the file instead of the pipeline you have above. Then in your while loop, increment $count only if $line =~ /$string/.


Randy.

commandline alternative:
perl -nle '$c++ if /foobar/;END{print $c}' /var/log/logfile

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