the data that needs to be replaced is just a bunch of paths to certain directories and it looks kinda like this #data to replace old/path/to/some/file another/old/path/to/some/file one/more/old/path/to/some/file #desired replacement new/path/to/some/file another/new/path/to/some/file one/more/new/path/to/some/file #### #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";