> > 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";
A more "perlish" method here than my first post: #!/usr/bin/perl use strict; use warnings; die "Two arguments required.\n" unless defined $ARGV[0] and defined $ARGV[1]; my $search_regex; eval { $search_regex = qr/$ARGV[0]/ }; if ($@) { die "Invalid regex passed as first argument: [EMAIL PROTECTED]"; } open F, $ARGV[1] or die "Can't open \"$ARGV[1]\" for reading: $!"; print scalar grep(/$search_regex/o, <F>), "\n"; close F; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>