open my $test, '<', 'test.txt' or die "Can't open file: $!"; while (my $line = <$test>) { # chomp not needed with split on ' '. my ($pattern, $cyclo, $group) = split ' ', $line, 4; print $line if 'FOLDING' eq $pattern && 'SHAPE' eq $cyclo && 'OXYGEN' eq $group; } }