So I was trying to understand this answer for merging two files using awk and I was coming up with my solution for a requirement of mine.
awk 'FNR==NR{a[$1]=$2 FS $3;next} {a[$1]=$2 FS $3}{ print a[$1]}' file2 file1
My files are as follows:-
file1 and file2 contents are as follows:-
1 xyz pqr F -
1 abc def A -
1 abc mno G -
1 abc def A
1 xyz pqr T
I am expecting an output as below:-
1 xyz pqr F - T
1 abc def A - A
Basically to match columns 1,2,3 from file2 on file1 and print append the content of the last column on file2 over the result.
So my understanding of the solution I did as follows,
FNR==NR{a[$1]=$2 FS $3;next}will process onfile2storing the entries of the arrayaas column2 space column3 till the end offile2.- Now on
file1, I can match those rows fromfile2by doing{a[$1]=$2 FS $3}which will give me all those rows infile1whose column$1's valuea[$1]is same as column2 value$2space column3 value$3. Now here comes the problem. - After having matched them in file1, I don't know how to print the values as expected. I tried printing
$0anda[$1]and they are giving me
outputs as sequentially,
1 xyz pqr F -
1 abc def A -
xyz pqr
abc def
respectively. My biggest concern was since I did not capture the last column from file2 during the FNR==NR pass, I may not have the value stored in my array? Or do I have it stored?
awk 'FNR==NR{a[$1 FS $2 FS $3]=$4;next} (($1 FS $2 FS $3) in a) {print $0, a[$1 FS $2 FS $3]}' f2 f1?a[...]=$4, that would have helped me! Please provide it as an answer, so that it will useful for reference!