Match or no match

Not sure if my attempt below is close but I am trying to match $1 of gene.txt to $2 of medicalexome.txt,
and if there is a match then $2 $1 are copied to a new result.txt which is tab-delimited. If no match is found then
$1 of gene.txt and "No Match" are copied to the result file.

 awk 'FNR==NR { E[$1]=$2 ; next }
{ $1=$1 in E?E[$1]:"No Match" } 1' medicalexome.txt gene.txt > result.txt OFS="/t"

So, the first name in gene.txt is A2m and that does not match anything in medicalexome.txt so A2M No Match is copied to result.txt

the 213 name in gene.txt is ALK and that matches medicalexome.txt so ALK 105590 is copied to result.txt

Thank you :).

Try something like this:

awk 'FNR==NR {a[$2]=$1;next}; {b=$1; if (b in a) print $1 "\t" a[$1]; else print $1 "\t" "No Match"};' medicalexome.txt gene.txt > result.txt

@OP: Modifications to your code:

awk 'FNR==NR { E[$2]=$1 ; next } { $2=$1 in E?E[$1]:"No Match" } 1' OFS="\t" medicalexome.txt gene.txt > result.txt 

Hi Scrutinizer,
Can you please educate me re: the below items in red? Why is $1 being set to $2 and what is E? doing?

awk 'FNR==NR { E[$2]=$1 ; next } { $2=$1 in E?E[$1]:"No Match" } 1' OFS="\t" medicalexome.txt gene.txt > result.txt

Hi mjf,
$2 is not being set to $1 .
If $1 is present as an index in E[] ( $1 in E ), $2 is being set to E[$1] ; otherwise $2 is being set to the string No Match .

2 Likes

Got it! Thanks Don. I was unfamilar with the conditional C operator.

You're welcome. condition ? TrueResult : FalseResult is a very powerful shorthand in both C and awk . Isn't this a great place to learn about the tools we all use?

Absolutely! I enjoy reading the posts that peek my interests, chiming in when I can offer a solution and learning from all the variations (there is always more than one way to do something). For readability (both for me and other developers), I tend to code with less "cryptic" shorthand. e.g. everyone knows what "if/else" condition does but ? and : would require some comments for those not versed in C.

1 Like

Yes I normally also would tend to not write it like that, as I think it is a bit too cryptic and I would at least use parentheses to make it clear.
But I tried to make as few changes as possible to the OP's attempt in order to show what it would take for it to work, because he wondered if it was close..

1 Like

Thank you both :).