#Code to read in file my $inputFile = 'file.txt'; my $fileContent = do { open(my $fileHandle, $inputFile ) or die "Could not open file '$inputFile' $!"; local $/; <$fileHandle>; }; #replace search paths with data in hash per key, $oldSearchPaths and %searchPaths are initialized elsewhere foreach my $key (keys %searchPaths){ $fileContent =~ s/\Q$oldSearchPaths\E/\n$searchPaths{$key}/; } #code to write to file my $filename = 'fileCopy.txt'; open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; print $fh $fileContent; close $fh; print "Writing to File Done! \n";