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;
Était-ce utile?

La solution

 $_ != /\d+/

is short for

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

Instead of != you need !~

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

Autres conseils

Isn't that just:

if (/bash/ && ! /\d+/)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top