in reply to Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)

You get what you have written:
-print if line doesn't match the first
-OR print if line doesn't match the second.

That means the first line is printed because it doesn't match leroy and the second is printed because it doesn't match brown. The third is printed for either reason, so double. I think you want a nice flag that is set at every line and reset at every match.
  • Comment on Re: Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)

Replies are listed 'Best First'.
Re: Re: Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)
by tye (Sage) on Jul 10, 2001 at 00:05 UTC
    #!/usr/bin/perl -w use strict; my @avoid= map qr/\Q$_\E/i, qw(leroy brown); my $log= "/home/psmith/logfile"; open( IN, "< $log" ) or die "Can't read $log: $!\n"; my $line; while( $line= <IN> ) { print $line unless grep $line =~ $_, @avoid; }

    If @avoid is quite large, then having grep always match against all of them might be worth avoiding. Maybe one day grep will be optimized for this case. (: Until then:

    while( $line= <IN> ) { for( @avoid ) { if( $line =~ $_ ) { print $line; last; } } }
    Update: except for that being backwards. Try:
    while( $line= <IN> ) { for( @avoid ) { if( $line =~ $_ ) { $line= ""; last; } } print $line; }

            - tye (but my friends call me "Tye")
      Your second code prints if in @avoid. I hope it's Ironic(TM).