Frage

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;
War es hilfreich?

Lösung

 $_ != /\d+/

is short for

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

Instead of != you need !~

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

Andere Tipps

Isn't that just:

if (/bash/ && ! /\d+/)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top