user231536
user231536

Reputation: 2711

Grep one field in each line of a file in another file, unifying results

I have a file A with contents

id_7 name_7
id_10 name_10
....

and another file B with all ids:

id_1 ssn_1
id_2 ssn_2
.....

I want to grep the first field (id_7, id_10...) in the second file, however I want the matches to be output like this:

id_1 ssn_1 name_1
....

How do I do this quickly (million of lines in each file) ?

Upvotes: 0

Views: 50

Answers (1)

zhenguoli
zhenguoli

Reputation: 2308

When join deal with each line of each file, it depends on the common field, so it should be sorted to have the common field to be the same.

join -j 1 <(sort -k 1 file1) <(sort -k 1 file2)

Upvotes: 2

Related Questions