Match and subtract two fields

Not sure quite how to do this, but I am trying to use $1 of LCH.txt (exact match) to look for a match in $1 of genes.txt. If a match is found then in a new file match.txt $1 $2 ($4-$3) are copied.

Example, the first record in LCH is PPT1 and that matches row 713, column 1 of genes.txt.

Match.txt (tab-deliminated)

PPT1     chr1     24761
 awk 'NR==FNR{A[$1]=$1; next}  A[$1]  {$1=$1 " " A[$1]}1' LCH.txt genes.txt > OFS="/t" match.txt 

Thank you :).

HI, try:

awk 'NR==FNR{A[$1]; next} $1 in A{print $1, $2, $4-$3}' OFS='\t' LCH.txt genes.txt > match.txt

--
Note: like in the other thread, pay attention to the slash in \t which is the other way around and the placement of OFS="\t" which should occur before the file names and certainly before redirection to the output file..

1 Like

Thank you :).