I need to add new ID to the file with old ID (column 7), I collected old ID / new ID pairs in a lookup file and I am trying to use awk to do the job, but something is not clicking.
My input file
ABC| 107|1440589221| -118.117167| 33.986333|10| 497476|1
ABC| 125|1440591215| -118.181000| 34.046833|10| 495713|1
I need to get this
ABC| 107|1440589221| -118.117167| 33.986333|10| 497476|1|10636872
ABC| 125|1440591215| -118.181000| 34.046833|10| 495713|1|10640836
My lookup file
497476|10636872
495713|10640836
My awk code (with all the debug I put in it)
awk 'BEGIN{old_key=0; FS=OFS="|"}
{
if (NR == FNR) {
old_key = $1;
printf "adding to array: arr[%i] = %s\n", old_key, $2;
arr[old_key] = $2;
}
else {
old_key = $7;
printf "\t\tlooking for:%d\n", old_key
if(old_key in arr) {
new_key = arr[old_key];
printf "\t\tfound %d\n", arr[old_key];
}
else {
printf "\t\tNOT found %d\n", old_key;
new_key = -1;
}
print $0, new_key;
}
}
END{
print "END";
for(x in arr) print x, arr[x]
}'
Run results:
adding to array: arr[497476] = 10636872
adding to array: arr[495713] = 10640836
looking for:497476
NOT found 497476
ABC| 107|1440589221| -118.117167| 33.986333|10| 497476|1|-1
looking for:495713
NOT found 495713
ABC| 125|1440591215| -118.181000| 34.046833|10| 495713|1|-1
END
497476|10636872
495713|10640836
I'd appreciate it very much if someone can point me in the right direction, thanks in advance.