Q:

Problem with output csv while comparing csv files and extracting columns

I am trying to compare two csv files based on their first and output first two columns of file 1 and second and third columns of file 2 to a new csv file
Example:
file1.csv  
asdf123,1  
adfg234,2  
asdf567,3

file2.csv  
asdf123,q,w  
asdf567,r,t  

desired output  
asdf123,1,q,w  
adfg234,2,NA  
asdf567,3,r,t 

I used the following code
awk -F, 'FNR==NR{a[$1]=$0;next};{if (i in a) {print a[$1] "," $2} else {print a[$1] "," "NA"}}' file1.csv file2.csv > output.csv

However, the command after else doesn't seem to work and has empty rows in the place and in addition, the out put is printed as follows (with new lines in between):
asdf123,1  
*blank space* q,w  

asdf567,3  
*blank space* r,t

I am new to scripting.  Can someone please guide me to fix this. 

A:

With bash, sort and GNU join:
join -t ',' -a 1 -e 'NA' <(sort file1.csv) <(sort file2.csv) -o 1.1,1.2,2.2,2.3

Output:

adfg234,2,NA,NA
asdf123,1,q,w
asdf567,3,r,t

See: man join

