How to extract a line based on the multiple fields in perl
I have below file: file1.txt and I want to extract line based on composite match of multiple fields meaning if the line has combination of all these fields only it should extract the line that contains those fields
tcp
10.11.38.224
10.185.34.240
9012
file1.txt
access firewall udp 10.14.90.111 240.230.111.222 10.13.45.21 255.255.230.240 eq 8443
access firewall tcp 10.11.38.224 255.233.212.111 10.185.34.240 244.255.240.211 eq 9012
#!/usr/bin/perl
open(SOURCE,"<file1.txt");
while (my @gitLines_mst = <SOURCE>)
{
my $fld0 = "tcp";
my $sIP = "10.11.38.224";
my $dIP = "10.185.34.240";
my $fld5 = "9012";
print "$fld0";
my @llll = grep {"/$fld0/" && "/$sIP/" && "/$dIP/" && "/$fld5"} @gitLines_mst;
print "here please: @llll \n";
}
I wrote the above script, it is listing out matched line along with the entire contents of file again
my output:
access firewall udp 10.14.90.111 240.230.111.222 10.13.45.21 255.255.230.240 eq 8443
access firewall tcp 10.11.38.224 255.233.212.111 10.185.34.240 244.255.240.211 eq 9012
I even removed double quotes while searching and tried:
my @llll = grep {/$fld0/ && /$sIP/ && /$dIP/ && /$fld5} @gitLines_mst;
I got the error:
Search pattern not terminated at ./sample line 11.
My desired output is:
access firewall tcp 10.11.38.224 255.233.212.111 10.185.34.240 244.255.240.211 eq 9012
/
after$fld5
on that line - i.e. the pattern isn't terminated, just as the error msg says. It should be/$fld5/
.