while($line=<>) { chomp $line; if($line =~ /^\$$/) { #look for start of line, followed by a literal dollar sign, followed by the end of the line (not newline character, but $ has to be at the end of $line) close OUTFILE; undef $data; next; } if(!$data) { open(OUTFILE,">$line") or die; } else { $data = 1; print OUTFILE "$line\n"; } }