howto 'cat file|grep "foo bar"|wc -l' in perl

From: Maurice Lucas (mslucas_at_taos-it.nl)
Date: 07/30/04


To: <beginners@perl.org>
Date: Fri, 30 Jul 2004 11:47:47 +0200

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";
---
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?
With kind regards
Met vriendelijke groet,
Maurice Lucas
TAOS-IT


Relevant Pages