open(OUTPUT,">matchingfile.txt")||die; open(OUTPUT2,">nonmatchingfile.txt")||die; while (<>) { chomp; if(/what you want to match/) { print OUTPUT $_ . "\n"; } else { print OUTPUT2 $_ . "\n"; } print OUTPUT $_ # note no semicolon here... unless /whatever condition matches the patterns you want/; } close(OUTPUT)|| die "Could not close $!\n"; close(OUTPUT2)|| die "Could not close $!\n";