Goal: To combine the data from two files into one file.
File1 = 11 fields, the first field is the unique key
File2 = 2 fields, the first field is the unique key
What I want to do is match File2:column1 with File1:column1 and if it matches, to add the data from File2:column2 to the matching line in File1 (in essence, creating an 12th column).
Field delimiter = pipe (|)
Sample data from File1:
3065462|245|1|Z682.4 .S89 B33 2007|3531301|0|10/20/2009|3531301|LIBR|CENTRAL|Complete.
3066405|245|1|jlk|3532264|0|10/20/2009|3532264|LIBR|CENTRAL|Energy.
Sample data from File2:
3065462|Westport, Conn. ; London : Libraries Unlimited, 2007.
3066405|Washington : U.S. Govt. Print. Off., 1976.
Desired output:
3065462|245|1|Z682.4 .S89 B33 2007|3531301|0|10/20/2009|3531301|LIBR|CENTRAL|Complete.|Westport, Conn. ; London : Libraries Unlimited, 2007.
3066405|245|1|jlk|3532264|0|10/20/2009|3532264|LIBR|CENTRAL|Energy.|Washington : U.S. Govt. Print. Off., 1976.
I've tried this, with no luck:
awk -F"|" 'BEGIN {
while ((getline < "file2") > 0)
f2array[$1] = $1
OFS="|"}
{if (f2array[$1])
print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,f2array[$2]
}' file1 > results-all
I've also tried changing $1 to $0, but that didn't work either. I've actually tried several things over the past several days, but either I get nothing or I get some kind of awk error:
awk: syntax error near line 4
awk: illegal statement near line 4
awk: syntax error near line 13
awk: bailing out near line 13
(from this script -
#!/bin/awk -f
BEGIN {
FS = "|"
while ((getline < ARGV[0]) > 0)
f2array[$1] = $1
OFS="|"}
{if (f2array[$1])
print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,f2array[$2]
else
print $1 " not listed in resultst" > "unmatched"
} ARGV[1]
./test.awk resultsp resultst)