Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting

awk, bash, grep, ksh

Thread Tools Search this Thread Display Modes
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:





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)


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
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 October 2016, 2:39 PM EDT
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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:

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12:27 AM.