The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparison of 2 files kamal_418 Shell Programming and Scripting 2 08-04-2008 06:05 AM
need some help..Comparison bluesilo Shell Programming and Scripting 0 02-23-2008 04:43 PM
Comparison of 2 files in UNIX Dana Evans UNIX for Dummies Questions & Answers 32 11-21-2007 07:05 AM
Comparison of two files in awk jerome Sukumar Shell Programming and Scripting 12 07-26-2006 08:16 AM
String Comparison between two files using awk rudoraj Shell Programming and Scripting 7 07-25-2006 11:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-06-2008
sourav1982 sourav1982 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Post Need help on comparison of two csv files

Dear All,

I want to compare two csv files using shell programming -

File 1 contents

44,,NONE,0,,2/2/1901
66,,NONE,0,,2/3/1901

File 2 Contents

1022,3708268,AUFX,0,100919,3/1/2006
66,,NONE,0,,2/3/1901

After comparing each column/field I want to print the occurances of difference for each field/column i.e suppose if 44 and 1022 doesn't match then it will be 1, suppose if it matches then it will be 0. My ultimate goal is to count the total number of differences for each field. Any help will be highly appreciated.
  #2 (permalink)  
Old 08-06-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question have you looked at diff & comm

diff = show differences in files
comm = show what is the same in files
  #3 (permalink)  
Old 08-06-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Code:
paste file1 file2 | awk -F '\t' '
{
  fnum1 = split($1,a,",")
  fnum2 = split($2,b,",")

  linediff=0
  f=0
  while ( ++f <= fnum1 ) {
    if ( a[f] != b[f] ) { ++diff[f]; ++linediff }
  }
}
END {
  while ( ++n <= fnum1 ) {
    print n,diff[n]+0
  }
}
'

Last edited by cfajohnson; 08-06-2008 at 05:35 PM..
  #4 (permalink)  
Old 08-06-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Hi,

Try this one.

input:
Code:
a:
a,30,c
b,30,c
d,27,c
b:
a,29,f
b,30,c
e,26,f
output:
Code:
Filed1 mismatch:1

Filed2 mismatch:2

Filed3 mismatch:2
code:

Code:
paste -d"," a b | awk 'BEGIN{FS=","}{
if($1!=$4)
	a++
if($2!=$5)
	b++
if($3!=$6)
	c++
}
END{
print "Filed1 mismatch:"a"\n"
print "Filed2 mismatch:"b"\n"
print "Filed3 mismatch:"c"\n"
}'
  #5 (permalink)  
Old 08-11-2008
sourav1982 sourav1982 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Comparing two csv files

Thanks a lot Summer for this code. This is working fine. I want to put the result in a log file. Thats why I have used echo "">log.txt. But this is not working. I have tried with print "">log.txt also but with no result. Any idea if I want to put this in a log file.
  #6 (permalink)  
Old 08-11-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Can you post a sample of the input and its output.
Closed Thread

Bookmarks

Tags
compareing two csv files, comparing each field of a csv file, finding the count difference

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0