my %xlate; # open DOC1 here while () { chomp; my($word_part) = split /_/; $xlate{$word_part} = $_; } # open DOC2 here while () { s/(\S+)/$xlate{$1}/ge; print; }