open RH, "2.txt" or die $!; /^(.+?)\s*$/ and push @new_content, $1 while ; close RH; open RH, "1.txt" or die $!; /^\d+\s*(.+?)\s*$/ and push @old_content, $1 while ; close RH; open WH, ">1.txt" or die $!; for(@new_content){ $count++; print WH "$_ $old_content[--$counter]\n" }