Domanda

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;
È stato utile?

Soluzione

 $_ != /\d+/

is short for

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

Instead of != you need !~

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

Altri suggerimenti

Isn't that just:

if (/bash/ && ! /\d+/)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top