Merging files with common IDs without JOIN

Hi, I am trying to merge information across 2 files. The first file is a "master" file, with all IDS. File 2 contains a subset of IDs of those in File 1.

I would like to match up individuals in File 1 and File 2, and add information in File 2 to that of File 1 if they appear. However, if an individual doesn't appear in File 2, I want to keep the data they have in File 1.

FILE 1

ID INFO1 INFO 2
PERSON1 A 3
PERSON2 B 2
PERSON3 C 11
PERSON4 D 10
PERSON5 E 6

FILE 2

ID INFO3
PERSON1 PPP
PERSON2 NNN
PERSON3 LLL

Desired output

ID INFO1 INFO2 INFO3 
PERSON1 A 3 PPP
PERSON2 B 2 NNN
PERSON3 C 11 LLL
PERSON4 D 10 0
PERSON5 E 6 0

If the data for INFO 3 doesn't exist, this should be replaced with a 0 in place of a value. Would anyone have any ideas? Unless I'm missing something obvious a simple join command won't work. An awk solution would be perfect but I'm currently drawing blanks. Any help would be very much appreciated!

awk 'NR==FNR{A[$1]=$2;next}{if(A[$1]){print $0,A[$1]}else{print $0,"0"}}' file2 file1
1 Like
join -a1 -o0 1.2 1.3 2.2 -e0 file1 file2
1 Like

Awesome, Pamu that worked perfectly, thank you.

balajesuri, that chucked up an error, but I am using a BSD version of join so that might be the reason why!

Cheers to you both