brucezepplin
brucezepplin

Reputation: 9752

join two files on first two columns and print columns from files

if I have the following:

file 1

1 a qpqp
2 b qpqp
3 c qpqp

file 2

1 a xkcd
2 b xkcd
3 d xkcd

I want to join both files where columns 1 and 2 match, and print the output side by side. i.e.

1 a qpqp 1 a xkcd
2 b qpqp 2 b xkcd

I can print just the first file columns using

awk 'NR==FNR{a[$1,$2]=$3;next} ($1,$2) in a{print $0, a[$1,$2]}' file1.txt file2.txt

But as I mentioned, I want to print the columns from both tables. Thanks!

extra info- the files will not be sorted beforehand (takes too long), and file 2 is MUCH bigger than file 1.

Upvotes: 0

Views: 328

Answers (1)

James Brown
James Brown

Reputation: 37394

In awk:

$ awk 'NR==FNR{a[$1 FS $2]=$0;next} (($1 FS $2) in a) {print a[$1 FS $2],$0}' file1 file2
1 a qpqp 1 a xkcd
2 b qpqp 2 b xkcd

Upvotes: 2

Related Questions