I'm a little stuck and would be grateful of some advice!
I have three files, two of which contain reference data that I want to add to a line of output in the third file. I can't seem to get awk to print array contents as I would expect.
The input files are:
# Input file
AAA,OAA,0313
AAB,OAB,0314
# Ref file1
NAA,123
NAB,456
# Ref file2
0313,123
081A,456
0314,789
I need to match a line in Ref File2 based on the last field of the main input file. I then need to match the last field in Ref File2 to Ref File1.
The output should then be:
AAA,OAA,0313,123,NAA
AAB,OAB,0314,456,NAB
Currently my code doesn't quite work as the last field of my output is empty. I have tried numerous field numbers and $NF for the f1 array, but can't seem to get any output. My code is:
FILE1=reffile1
FILE2=reffile2
FILE3=inputfile
awk -F"," -v "file1=$FILE1" -v "file2=$FILE2" '
FILENAME == file1 {
f1[$2] = $0
next
}
FILENAME == file2 {
f2[$1] = $2
next
}
{
if ( $NF in f2 ) {
if ( f2[$NF] in f1 ) { ## I'm not sure why $NF works here and $2 does not, but I seem to get the output I want..
print $0","f2[$NF]","f1[$1] # This line does not produce the relevant output from the f1 array (I want field 1 to print)
}
}
}' $FILE1 $FILE2 $FILE3
Can anyone suggest why this is not working and how the above can be modified to work. I seem to be 95% of the way there, I just would like to understand why this is not working and how output is taken from an array.
Thanks.