open my $cmd, "/bin/cmd|" or die "something useful here like $^E"; while (<$cmd>){ chomp; print $append_file "$1\n" if $_ =~ /_id:(\S)+$/; #please re-check the regex.. }