Question

I want to print a line of a file if it contains string A and it doesnt contain string B (also splitting into newlines at each colon). What is the proper syntax? Here is what I tried ( I want it print lines containing "bash" but to not print lines containing numbers):

my $file = passwdtest;
open(FH, "$file"); 
foreach (<FH>) { 
print join("\n", split(/:/, "$_")) if ($_ =~ /bash/ and $_ != /\d+/);
};

close FH;
Was it helpful?

Solution

 $_ != /\d+/

is short for

 $_ != ($_ =~ /\d+/)

Instead of != you need !~

if ($_ =~ /bash/ and $_ !~ /\d+/);

OTHER TIPS

Isn't that just:

if (/bash/ && ! /\d+/)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top