Comparing 2 CSV files and sending the difference to a new csv file | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Tags
bash, grep awk, ksh bash, unix code

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-13-2013
Naresh101 Naresh101 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 14 May 2013, 1:47 AM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
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

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


file2.csv

Code:
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)

Code:
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.

Last edited by Franklin52; 03-14-2013 at 03:20 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old Unix and Linux 03-13-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 March 2015, 2:02 PM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts
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:

Code:
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=,

The Following User Says Thank You to Yoda For This Useful Post:
Naresh101 (03-15-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare two CSV files and put the difference in third file with line no,field no and diff value. karingulanagara Shell Programming and Scripting 12 03-05-2013 04:01 AM
Comparing 2 difference csv files bobby1015 Shell Programming and Scripting 2 08-24-2012 02:51 PM
comparing csv files sukhdip Shell Programming and Scripting 3 12-21-2011 05:29 AM
Comparing Strings in 2 .csv/txt files? chickeneaterguy Shell Programming and Scripting 13 02-18-2011 03:32 AM
Need to compare two csv files values and write into another csv file chinnahyd Shell Programming and Scripting 2 02-06-2009 01:17 PM



All times are GMT -4. The time now is 01:19 PM.