I thought I could do a join on file 1 and file 2, however join will insert file 2 into the first two rows, and incorrect merge the file with the -a1 flag.
I figured I could join and simply pipe it to awk to do a stupid print of only the specific columns I need, but join didn't work right for me.
I tried awk, but I've only managed to append the values to all of the rows.
i'm working through vgersh99's solution right now so will need to try and get it working with the actual example (so I can modify it going forward).
join -t"," -1 2 -2 1 file1.txt file2.txt
that gives me
A, 1, Bla, 123, 1, Aha
B, 2, Bla2, 123, 2, Ok
C, 3, Bla3, 123
D, 4, Bla4, 123
which is not expected.
vgersh99: thanks for the reply
can I ask what
FNR==NR{f2[$1]=$2;next} --copies the second value from file 2 into an array
$2+0 in f2 -- What does this line do?
{$3=f2[$2+0]}1' --this affects file1, if the $3 file matches your value in f2, print buffer
OFS=, file2 file1
FNR==NR{f2[$1]=$2;next} --copies the second value from file 2 into an array
# FNR==NR is true for the FIRST file to be processed
# f2 - an array index by the value of the FIRST field with the value of the SECOND field (from file2)
$2+0 in f2 -- What does this line do?
# We get here we already processed file2 - here we're dealing with file1
# $2+0 - the value of the SECOND field. As it seems that you have a leading spaces in file 1,
# by adding '0' we convert a field value to an int
# $2+0 in f2 - the value of the second field of file1 is in array 'f2'
# '$2+0' is one of the indexes (not values) of array f2
#
{$3=f2[$2+0]}1' --this affects file1, if the $3 file matches your value in f2, print buffer
# assign the the value from array f2 indexed by '$2+0' to the value of the THIRD field.
OFS=, file2 file1