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).
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
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?
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.
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