use warnings 'all'; use strict; use autodie; open my $input, '<', 'file1.txt'; open my $input2, '<', 'file2.txt'; open my $out, '>', 'output.txt'; while ( my $sentence = <$input> ) { my $substring = '\.c.*?entry'; $sentence =~ s{$substring}{$file2}; print $out , $sentence; }