use strict; use warnings; my $in_file_name = 'sample.txt'; { local $/ = '.slow'; open my $input, '<', $in_file_name or die $!; while( my $rec = <$input> ) { chomp $rec; next unless $rec =~ m/(\d+)\.pace(.+)$/s; my( $out_file_name, $capture ) = ( $1, $2 ); open my $output, '>', $out_file_name or die $!; print $output $capture; close $output or die $!; } close $input; }