my $file1 = '/path/to/file/one'; my $file2 = '/path/to/file/two'; open FILE1, $file1 or die $!; my @content1 = ; close FILE1; open FILE1, '>'.$file1 or die $!; foreach my $line (@content1) { if ($line =~ m~^(.*#REPLACE)~) { open FILE2, $file2 or die $!; print FILE1 $1,(); close FILE2; last; } print FILE1 $line; } close FILE1;