Using awk
is better here:
awk -v OFS=_ 'NR==FNR{a[$1]=$2;next} $1 in a{$1=a[$1]} 1' names.txt FS=_ file.txt
john.D_trait1 8.5
mary.D_trait2 2.2
kelly.O_trait1 9.2
john.D_trait2 1.3
mary.D_trait1 10.0
kelly.O_trait2 2.1
Where names.txt
is this:
Sample3 john.D
Sample6 mary.D
Sample7 kelly.O