Replace pattern with file content

Tag(s):

NOT WORK: String with ampersand.

# file2 is the input file content
 
sed -e "s/localhost/$(sed 's:/:\\/:g' file2)/" file1
 
awk 'FNR==NR{s=(!s)?$0:s RS $0;next} /<<CODE>>/{sub(/<<CODE>>/, s)} 1' file2 file1