perl - Grep elements from array that exists in output -


is there way use grep find elements exists in specific array? example :

my @ips ={"10.20.30","12.13.14","30.40.50"};  $cmd = `netstat -aa | grep -c ips[0] or ips[1] or ips[2]  `  print "$cmd"; 

i want cmd return number of ips (only found in array) exists in output of netstat command. know can use " | " or condition assume not know number of elements in array.

your @ips array not contain think contains. think wanted:

my @ips = ("10.20.30","12.13.14","30.40.50"); 

and i'd write as:

my @ips = qw(10.20.30 12.13.14 30.40.50); 

i know can use " | " or condition assume not know number of elements in array

i don't think matters @ all.

# need quotemeta() escape dots $ip_str = join '|', map { quotemeta $_ } @ips; $ip_re  = qr/$ip_str/;  # keep of processing possible in perl-space @found = grep { /$ip_str/ } `netstat -aa`; scalar @found; 

an alternative regex, turn @ips hash.

my %ip = map { $_ => 1 } @ips; @found = grep { $ip{$_} } `netstat -aa`; scalar @found; 

update: actually, last example doesn't work. need extract ip addresses netstat output before matching against hash. i've left there in case inspires expand it.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -