Awk Compare f1,f2,f3 of File1 with f1 of File2

I have an Awk string-compare problem and have searched the internet and forums for a solution i could use but cannot find a solution i understand to make work with my particular problem:

I need to compare (field1 field2 field3 of File1) against (field1 of File2) and if they match print out (field1:field2:field3) of File1 followed by field2 of File2 and field3 of File2. In the case of no-match exists in File2, just print out the (field1:field2:field3 of File1).

File1

RICHARD:J:LOONEY
JAMES:F:BIXLER
JED:H:YOUNG
LEWIS:A:ZAPP
SILAS: :VECINIO
DERICK:S:HOLMER
MICK: :REZNIC
R: :BAKER
HERROD:G:LOST
OLIVE:N:TORROSSI
JASPER:G:WILCOX
AUDREY:H:VIKING

File2

RICHARD J LOONEY:YONKERS:NY  
JAMES F BIXLER:LEXINGTON:KY 
JED H YOUNG:SURREY:BC
LEWIS A ZAPP:GREEN VALLEY:CA
SILAS VECINIO:COLUMBUS:OH 
DERICK S HOLMER:WESTFORD:MA 
MICK REZNIC:AKRON:OH  
R BAKER:AUCKLAND:NEW ZEALAND
OLIVE N TORROSSI:DAVISON:MI
LEWIS A ZAPP:GREEN VALLEY:CA
JASPER G WILCOX:CANTON:OH 
AUDREY H VIKING:SURREY:BC

DESIRED OUTPUT

RICHARD:J:LOONEY:YONKERS:NY
JAMES:F:BIXLER:LEXINGTON:KY
JED:H:YOUNG:SURREY:BC
LEWIS:A:ZAPP:GREEN VALLEY:CA
SILAS: :VECINIO:COLUMBUS:OH 
DERICK:S:HOLMER:WESTFORD:MA 
MICK: :REZNIC:AKRON:OH  
R: :BAKER:AUCKLAND:NEW ZEALAND
HERROD:G:LOST
OLIVE:N:TORROSSI:DAVISON:MI
JASPER:G:WILCOX:CANTON:OH
AUDREY:H:VIKING:SURREY:BC

You can do something like that (assume that the two files two contain the same list of names) :

function are_same_names(n11, n12, n13, n2     ,n1) {
   n1 = n11 " " n12 " " n13;
   gsub(/ +/, " ", n1);
   return (n1 == n2)
}

BEGIN {
   FS = OFS = ":";

   while (getline < ARGV[1]) {
      f11 = $1;
      f12 = $2;
      f13 = $3;
      getline < ARGV[2];
      if (are_same_names(f11, f12, f13, $1)) {
         print f11, f12, f13, $2, $3;
      } else {
         print f11, f12, f13;
      }
   }
}

Command:

awk -f compare.awk File1 File2

Jean-Pierre.

Thanks for your help and possible solution! After running your code against my two example files above it returns:

RICHARD:J:LOONEY:YONKERS:NY  
JAMES:F:BIXLER:LEXINGTON:KY 
JED:H:YOUNG:SURREY:BC
LEWIS:A:ZAPP:GREEN VALLEY:CA
SILAS: :VECINIO:COLUMBUS:OH 
DERICK:S:HOLMER:WESTFORD:MA 
MICK: :REZNIC:AKRON:OH  
R: :BAKER:AUCKLAND:NEW ZEALAND
HERROD:G:LOST
OLIVE:N:TORROSSI
JASPER:G:WILCOX:CANTON:OH 
AUDREY:H:VIKING:SURREY:BC

When you said

Do you mean the names are getting compared only once by line number, like Line1 File1 field1 is only compared to Line1 File2 field1 where it either matches or not, then goes on to Line2 File1 field1 compared to Line2 File2 field1 and matches or not, and then goes on to next line, etc, to the EOF?

Yes.
If the two files may contains different names the awk program must be modified (the function are_same_names is allways valid).

Jean-Pierre.

OK, that explains why i never could find a match in my real files using the above code, as the names in File1 i'm trying to match could be on any line in File2; so as you say i'll have to try to modify it to scan through every line in File2.

Thanks for getting me this far!

Try and adapt the following awk program :

BEGIN {
   FS = ":";
}
NR==FNR {
   Names[$1] = $2 ":" $3;
   next;
}
{
   name = $1 " " $2 " " $3;
   gsub(/ +/, " ", name);
   if (name in Names) {
      print $0 ":" Names[name];
   } else {
      print $0 "
   }
}

Command;

awk -f compare.awk File2 File1

Jean-Pierre.

Hi,
This one should be ok for you.

input:

a:
line1:a:A
line2:b:B
line3:c:C
line4:d:D
line5:e:E
b:
line6:f:F
line3:cc:CC
line5:ee:EE
line2:bb:BB

output:

line1:a:A
line2:b:B:bb:BB
line3:c:C:cc:CC
line4:d:D
line5:e:E:ee:EE

code:

nawk 'BEGIN{FS=":"}
{
if (NR==FNR)
	line[$1]=$0
else
	if (line[$1]!="")
	line[$1]=sprintf("%s:%s:%s",line[$1],$2,$3)
}
END{
for(i in line)
	print line 
}' a b