open my $cmd, "ncks -v $variable -d station,$id,$id $file|" or die "unable to open a CMD for read!"; while (<$cmd>) { next unless $_ =~ /station\[/; # put your grep logic in perl side too! chomp; ...