auto_w has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for replies but i still can't make it--the below lists file with some lines in it i need to match a combination of 1=NF and 5=8( each line starts with 8=F^) to select a right line
8=F^A 9=145^A 5=D^A 34=4^A 49=ABC_DEFG01^A 52=20090323-15:40:29^A 56=C +CG^A 115=XYZ^A 1=NF 0542/03232009^A 54=1^A 38=100^A 55=CVS^A 40=1^A 5 +9=0^A 47=A^A 60=20090323-15:40:29^A 21=1^A 207=N^A 10=139^A 8=F^A 9=226^A 5=8^A 128=XYZ^A 34=4^A 49=CCG^A 56=ABC_DEFG01^A 52=20090 +323- 15:40:35^A 55=CVS^A 37=NF 0542/03232009^A 1=NA 0542/03232009^A 1 +7=0^A 20=0^A 39=0^A 150=0^A 54=1^A 38=100^A 40=1^A 59=0^A 31=0^A 32=0 +^A 14=0^A 6=0^A 151=100^A 60=20090323- 15:40:30^A 58=New order^A 30=N +^A 207=N^A 47=A^A 10=149^A 8=F^A 9=378^A 5=8^A 128=XYZ^A 34=5^A 49=CCG^A 56=ABC_DEFG01^A 52=20090 +323- 15:40:35^A 55=CVS^A 37=NF 0542/03232009^A 1=NF 0542/03232009^A 1 +7=NF 0542/03232009 001001001^A 20=0^A 39=2^A 150=2^A 54=1^A 38=100^A +40=1^A 59=0^A 31=25.4800^A 32=100^A 14=0^A 6=0^A 151=0^A 60=20090323- +15:40:30^A 58=Fill^A 30=N^A 76=0034^A 207=N^A 47=A^A 9430=NX^A 9483=0 +00008^A 9578=1^A 382=1^A 375=TOD^A 337=0000^A 437=100^A 438=1243^A 95 +79=0000100001^A 9433=0034^A 29=1^A 63=0^A 9440=001001001^A 10=080^A #!/usr/bin/perl -w use strict; use warnings; my $log = "log"; my $log_string = "grep '^8=' $log |"; usage (); chomp (my $TAB_1 = $ARGV[0]); chomp (my $TAB_5 = $ARGV[1]); open (my $HAN, "$log_string") || die "Problems grepin... : $!"; my @contents = <$HAN>; my @matches = grep /1=$TAB_1 && 5=$TAB_5/, @contents ; foreach (@matches) { print "[+] FOUND $TAB_1 and $TAB_5 in\n", "-----------------------\n", "$_" ;} now you can do it like foreach (@contents) if /1=$TAB_1 && 5=$TAB_5 / - that also doesn't find lines with those 2 + matches

Replies are listed 'Best First'.
Re: Perl:how to find multiple strings
by wind (Priest) on Apr 08, 2011 at 16:19 UTC
    Use the block form of grep and separate your regexes:
    my $matches = grep {/1=$TAB_1/ && /5=$TAB_5/} @contents ;
    Also, the question is, are those variables supposed to be literal strings? If so, then you need to use quotemeta or \Q..\E to escape regular expression special characters:
    my $matches = grep {/1=\Q$TAB_1\E/ && /5=\Q$TAB_5\E/} @contents ;
    And finally, if those strings are supposed to match in a particular order, then just put them back in a single regex with .* between them, not &&:
    my $matches = grep {/1=\Q$TAB_1\E.*5=\Q$TAB_5\E/} @contents ;
Re: Perl:how to find multiple strings
by jpl (Monk) on Apr 08, 2011 at 17:15 UTC

    In addition to what wind had to suggest

    my $matches = grep {/1=\Q$TAB_1\E/ && /5=\Q$TAB_5\E/} @contents ;
    you'll want to do something to prevent /1=\Q$TAB_1\E/ from matching 21=, and so on. Probably something as simple as putting a blank in front of the 1= and 5=, in your context.

Re: Perl:how to find multiple strings
by jwkrahn (Abbot) on Apr 08, 2011 at 17:30 UTC

    You need a program something like this:

    #!/usr/bin/perl use strict; use warnings; my $log = 'log'; @ARGV == 2 or die "usage: $0 TAB_1 TAB_5\n"; my ( $TAB_1, $TAB_5 ) = @ARGV; open my $HAN, '<', $log or die "Cannot open '$log' because: $!"; while ( <$HAN> ) { next unless /^8=/; print "[+] FOUND $TAB_1 and $TAB_5 in\n", "-----------------------\n", $_ if / 1=\Q$TAB_1\E / && / 5=\Q$TAB_5\E /; }
      still doesn't make any output --does it work for you?

        It apparently doesn't work because the string "5=8(" is not in any of the lines.

Re: Perl:how to find multiple strings
by anonymized user 468275 (Curate) on Apr 08, 2011 at 16:01 UTC
    I can't cope with your code - blame it on Rio - code tags notwithstanding and I don't believe it would run at all without compilation errors, but I do see some definite holes I can categorise...
    my $matches = grep ... foreach ( @matches ) { ...
    should be my @matches. // are pattern match delimiters but the enclosed expression is not likely to be accepted as a pattern. Perhaps you could tell us what strings in the logfile you are trying to detect.

    One world, one people

Re: Perl:how to find multiple strings
by eff_i_g (Curate) on Apr 08, 2011 at 15:51 UTC