Frederica
Frederica

Reputation: 187

awk not print from the first file when merging two files

I'm trying to merge two files based on specific columns. I used code as below

awk 'NR==FNR{a[$2]=$3; next}$1 in a{print a[$2], $0}' temp2.txt temp3.txt

My temp2.txt looks like

0 a1 0 G T
0 a2 0 A T
0 a3 0 C T

and my temp3.txt looks like

a1 G
a2 T
a3 C

My output is as below

 a1 G
 a2 T
 a3 C

while my expected out put is

0 a1 G
0 a2 T
0 a3 C

Is there any reason why matched result from the first file doesn't print?

Upvotes: 1

Views: 181

Answers (1)

MauricioRobayo
MauricioRobayo

Reputation: 2356

You can also do this with the join command:

join -o 1.1 1.2 2.2 -1 2 -2 1 temp2.txt temp3.txt

That is: output column 1 and column 2 from file 1 and column 2 from file 2 (-o 1.1 1.2 2.2), joining on column 2 from file 1 and column 1 from file two (-1 2 -2 1).

The awk alternative to print all columns from file one (temp2.txt) can be something like this:

awk 'NR==FNR{a[$1]=$2; next}$2 in a{print $0, a[$2]}' temp3.txt temp2.txt

Output:

0 a1 0 G T G
0 a2 0 A T T
0 a3 0 C T C

Upvotes: 2

Related Questions