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   -   Original Discussion by Naresh101
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 04:20 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
Old Unix and Linux 03-13-2013   -   Original Discussion by Naresh101
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
Join Date: Jan 2012
Last Activity: 19 November 2017, 3:37 PM EST
Location: Galactic Empire
Posts: 3,601
Thanks: 253
Thanked 1,315 Times in 1,232 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 05:01 AM
Comparing 2 difference csv files bobby1015 Shell Programming and Scripting 2 08-24-2012 03:51 PM
comparing csv files sukhdip Shell Programming and Scripting 3 12-21-2011 06:29 AM
Comparing Strings in 2 .csv/txt files? chickeneaterguy Shell Programming and Scripting 13 02-18-2011 04:32 AM
Need to compare two csv files values and write into another csv file chinnahyd Shell Programming and Scripting 2 02-06-2009 02:17 PM

All times are GMT -4. The time now is 05:45 PM.