Merge two files matching columns

Hi!

I need to merge two files when col1 (x:x:x) matching and adds second column from file1.txt.

[root@test]# cat 1.txt 
aaa;a12
bbb;b13
ccc;c33
ddd;d55
eee;e11
[root@test]# cat 2.txt 
bbb;b55;34444;d55
aaa;a15;35666;a44

I try with this awk and I get succesfully first column from 1.txt:

[root@test]# awk -F";" 'NR==FNR{a[$1  ]=$1;next} ($1 ) in a {print $1,$2,a[$1]}' 1.txt 2.txt
bbb b55 bbb
aaa a15 aaa

But I need second column from 1.txt like this output :

bbb b55 b13
aaa a15 a12

But when I try with a[$2] is a empty variable:

[root@test]# awk -F";" 'NR==FNR{a[$1  ]=$1;next} ($1 ) in a {print $1,$2,a[$2]}' 1.txt 2.txt
bbb b55 
aaa a15

Why? Any help please?

Best regards,
FHL

You would need a[$1]=$2 and $1 in a {print $1,$2,a[$1]}

1 Like

Perfect!!! I'm was turning crazy.

Many thanks!