open SEARCH, $file_to_search or die $!; my $count = 1; while () { if (/$regex_to_search_for/) { open FOUND, '>', "found$count.txt" or die $!; print FOUND $_; ++$count; } }