-1

How can I print all matching lines between two files including the duplicates

sourcefile:

SNP value
RS1 0.55
RS2 0.52
RS3 0.25
RS4 0.22

queryfile:

SNP
RS1
RS2
RS2
RS3
RS4
RS4

Desired output:

SNP value
RS1 0.55
RS2 0.52
RS2 0.52
RS3 0.25
RS4 0.22
RS4 0.22

At present I am using the following command to retrieve the matching lines but is removing the duplicates, while i need to print all lines including the duplicates,

Any suggestions please

Thanks

awk 'BEGIN{FS=OFS="\t"}NR==FNR{a[$1]=$0;next}a[$1]{print}' queryfile sourcefile > output

marked as duplicate by Jeff Schaller, dr01, Anthon, chaos, Archemar Apr 12 '16 at 15:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

The join command will join these two files together for you

join sourcefile queryfile

Output (from your example files)

SNP value
RS1 0.55
RS2 0.52
RS2 0.52
RS3 0.25
RS4 0.22
RS4 0.22
1

It's essentially just a matter of processing the files in the opposite order

$ awk 'BEGIN{FS=OFS="\t"} NR==FNR{a[$1]=$0;next} a[$1]{print a[$1]}' sourcefile queryfile
SNP value
RS1 0.55
RS2 0.52
RS2 0.52
RS3 0.25
RS4 0.22
RS4 0.22

Not the answer you're looking for? Browse other questions tagged or ask your own question.