Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below:

file1.csv

ID,version,cost                        
1000,1,30     
2000,2,40     
3000,3,50     
4000,4,60  

file2.csv

ID,version,cost     
1000,1,30       
2000,2,45     
3000,4,55   
6000,5,70   

The expected o/p is a new csv file say - file3.csv should contain the details of IDs which are present in both the files but with some of the data related to it being different (here version and cost) - and the o/p should be as shown below.

o/p needed (o/p format)

ID,field,old,new  
2000,cost,40,45     
3000,version,3,4    
3000,cost,50,55 

I need a unix bash/ksh script for doing this. Please help me out.

First of all this thread is opened in the wrong forum: How to Post in the The UNIX and Linux Forums. Admins / Mods, please move this thread to correct forum!

Here is a solution using join and awk:

join -t"," -1 1 -2 1 -a1 file1.csv file2.csv | awk -F, ' BEGIN {
        print "ID,field,old,new"
} NF > 3 {
        if ( $3 != $5 )
                print $1, "cost", $3, $5
        if ( $2 != $4 )
                print $1, "version", $2, $4
} ' OFS=,
1 Like