open(IN, "temp.out"); ## Search for FTN my %values; # read through the input fileto get the values while() { if(/(.+?)<\/FTN>/) { #no need to quote <, >, ", = $values{$1} = $2; } } close(IN); open(IN, " ) { s///; # get rid of existing tags - we already have the values from them s/(.*)(<\/X-REF>)/$1$values{$2}<\/FTN>$3/; # alter the X-REF LINE print OUT; } close(IN); close(OUT);