Match based on criteria to file

Trying to match $1 of target.txt to $5 of file.txt. If there is a match then in an output.txt file $1,$1 (row underneath),$6,$4,$7 from file.txt are printed on the same line as $1 of target.txt. The input is from excel and the output should be tab-deliminated. Thank you :).

target.txt

PXL-A0000005
PXL-A0000007
PXL-A0000014 

file.txt

RefPrimer	ref	antiref	omosome	PrimerSet	SeqRxn	Link 
AntirefPrimer	antiref	ref	omosome			 
PXL-A0000005ref	69066	69311	1	PXL-A0000005	SeqRxn4	 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000005&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000005antiref	69311	69066	1			 
PXL-A0000007ref	69700	70033	1	PXL-A0000007	SeqRxn4	 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000007&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000007antiref	70033	69700	1			 
PXL-A0000014ref	861093	861418	1	PXL-A0000014	SeqRxn4	 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000014&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000014antiref	861418	861093	1 

Desired output

PrimerSet	RefPrimer	AntirefPrimer	SeqRxn	Chr	Link
PXL-A0000005 PXL-A0000005ref PXL-A0000005antiref SeqRxn4 1 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000005&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000007 PXL-A0000007ref PXL-A0000007antiref SeqRxn4 1 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000007&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000014 PXL-A0000014ref PXL-A0000014antiref SeqRxn4 1 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000014&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube				

 awk '$1 = $5  && $1 != "" {print $1,$1,$6,$4,$7}' FS="\t" OFS="," target.txt file.txt . output.txt 

A Perl solution

perl -ne 'BEGIN{open $refs ,"<","target.txt";@refs=<$refs>;chomp(@refs);$regex=join"|",@refs;}
@r=split/\s+/,$_;if ($r[4]=~/^$regex$/ ){$next = readline; @n=split/\s+/,$next;print join "\t",$r[4],$r[0],$n[0],@r[5,3,6],"\n"}' file.txt
PXL-A0000005    PXL-A0000005ref    PXL-A0000005antiref    SeqRxn4    1    https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000005&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube    
PXL-A0000007    PXL-A0000007ref    PXL-A0000007antiref    SeqRxn4    1    https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000007&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube    
PXL-A0000014    PXL-A0000014ref    PXL-A0000014antiref    SeqRxn4    1    https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000014&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube        
1 Like

Thank you :).... works great