Perl:how to find multiple strings

Hi , i'm trying to find lines in a file that have 2 matches,but i have trouble with regex -it doesn't search for a second string-please help

#!/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

Hi.

The code you posted had translation errors. I wrote a driver script to run a corrected version:

#!/usr/bin/env bash

# @(#) s1	Demonstrate driver for perl code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl

FILE=${1-data1}
pl " Data file $FILE:"
cat $FILE

pl " Results of ./p1 a z $FILE:"
./p1 a z $FILE

exit 0

calling the perl code:

#!/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]); 
shift ; shift ;
# open (my $HAN, "$log_string") || die "Problems grepin... : $!";
# my @contents = <$HAN>;
# my $matches = grep /1=$TAB_1 && 5=$TAB_5/, @contents ;
my @contents = <>;
# my @matches = grep /1=$TAB_1 && 5=$TAB_5/, @contents ;
my @matches = grep /$TAB_1/ && /$TAB_5/, @contents ;
 foreach (@matches) 
   { 
      print "[+] FOUND $TAB_1 and $TAB_5 in\n",
"-----------------------\n",
"$_" ;}

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
perl 5.10.0

-----
 Data file data1:
a
b
a b
x
a z
x y
z

-----
 Results of ./p1 a z data1:
[+] FOUND a and z in
-----------------------
a z

Best wishes ... cheers, drl

1 Like

Well Done -Nice ONE!!!