Re: newbie question about regex
Hello, I call my script with the following line $ ./count.pl /var/log/file text this works fine but sometimes the "text" is "foo(bar)" and then my scripts gives an error. syntax error near unexpected token `foo(b' I believe the syntax error is from your shell and you can get around this by quoting your string to search on... $ ./count.pl /var/log/file 'foo(bar)' Should do it. Sometimes,... even the most simple problems could take several hours to understand until you ask and somebody's gives th right answer, Thanks everybody for the quick and right answer With kind regards, Maurice Lucas TAOS-IT -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
newbie question about regex
Hello, I call my script with the following line $ ./count.pl /var/log/file text this works fine but sometimes the "text" is "foo(bar)" and then my scripts gives an error. syntax error near unexpected token `foo(b' Could somebody give me a hint? I'm working on linux My script #!/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/qmail/smtpd/rejects/[EMAIL PROTECTED] /var/log/qmail/smtpd/rejects/current"; my $logfile = $ARGV[0]; my $string = $ARGV[1]; my $tai_cmd = "/usr/local/bin/tai64n2tai"; my $count = 0; my $now = time; my $before = $now-86400; # Open a file like this open( LOG, "cat $logfile|$tai_cmd|" ) or die "Cannot open '$logfile': $!\n"; while( ) { my $line = $_;# $_ contains the current line of LOG my $entrytime = substr($line,0,10); if ($entrytime <= $now && $entrytime >= $before) { ++$count if ( $line =~ m/$string/ ); } } print "$string\n$count\n"; with kind regards, Met vriendelijke groet, Maurice Lucas TAOS-IT -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
Re: howto 'cat file|grep "foo bar"|wc -l' in perl
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 = ) { > > $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( ) { > 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>
howto 'cat file|grep "foo bar"|wc -l' in perl
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 = ) { $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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>