Compare two CSV files and put the difference in third file with line no,field no and diff value.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two CSV files and put the difference in third file with line no,field no and diff value.
# 1  
Old 03-04-2013
Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level.


For Example,


File 1:


A,B,C,D,E,F
1,2,3,4,5,6


File 2:

A,C,B,D,E,F
1,2,4,5,5,6


out put file:

Line 1;Field 2 diff Value B=C
Line 1;Field 3 diff Value C=B
Line 2;Field 3 diff Value 3=4
Line 2;Field 4 diff Value 4=5
etc.....

Please any one can help

I used diff but was only able to get line numbers.I als need field numbers and diff values.
# 2  
Old 03-04-2013
Code:
$ awk -F, 'NR==FNR{for(i=1;i<=NF;i++){A[i,NR]=$i}next}
{for(i=1;i<=NF;i++){if(A[i,FNR]!=$i){print "Line",NR";Field",i,"diff value",A[i,FNR]"="$i}}}' file1 file2

Line 3;Field 2 diff value B=C
Line 3;Field 3 diff value C=B
Line 4;Field 3 diff value 3=4
Line 4;Field 4 diff value 4=5

This User Gave Thanks to pamu For This Post:
# 3  
Old 03-04-2013
when i run the above command i am getting not found error
# 4  
Old 03-04-2013
What not found ? command?

Post the error here!!.
# 5  
Old 03-05-2013

rwxd256:dotisdm1:/home/corebank: $ cd /opt/DataMigration/data/NKARING
rwxd256:dotisdm1:/opt/DataMigration/data/NKARING: $ awk -F, 'NR==FNR{for(i=1;i<=NF;i++){A[i,NR]=$i}next}
continue...> ";Field",i,"diff value",A[i,FNR]"="$i}}}' taxdetails.csv RC023_test.csv <
ksh: $: not found
rwxd256:dotisdm1:/opt/DataMigration/data/NKARING: $


---------- Post updated at 12:50 AM ---------- Previous update was at 12:48 AM ----------

when i run this command in unix i got this error. i also used it in script
# 6  
Old 03-05-2013
Quote:
Originally Posted by karingulanagara

rwxd256:dotisdm1:/home/corebank: $ cd /opt/DataMigration/data/NKARING
rwxd256:dotisdm1:/opt/DataMigration/data/NKARING: $ awk -F, 'NR==FNR{for(i=1;i<=NF;i++){A[i,NR]=$i}next}
continue...> ";Field",i,"diff value",A[i,FNR]"="$i}}}' taxdetails.csv RC023_test.csv <
ksh: $: not found
rwxd256:dotisdm1:/opt/DataMigration/data/NKARING: $


---------- Post updated at 12:50 AM ---------- Previous update was at 12:48 AM ----------

when i run this command in unix i got this error. i also used it in script
It looks like you have done some mistakes while copying the code.
Where is a start bracket after next.???
where is print..? where is for loop..?

Please copy the code correctly and then post the results.
# 7  
Old 03-05-2013

#!/bin/bash
$awk -F, 'NR==FNR{for(i=1;i<=NF;i++){A[i,NR]=$i}next}
{for(i=1;i<=NF;i++){if(A[i,FNR]!=$i){print "Line",NR,";Field",i,"diff value",A[i,FNR]"="$i}}}' taxdetails.csv RC023_test.csv
i am using this in script and named it as Diff.sh


but when i run this script it is giving error as

rwxd256:dotisdm1:/opt/DataMigration/data/NKARING: $ Diff.sh
Diff.sh: line 2: -F,: command not found
rwxd256:dotisdm1:/opt/DataMigration/data/NKARING: $


I am new to unix please help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

2. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

3. Shell Programming and Scripting

Compare 2 csv files in ksh and o/p the difference in 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... (7 Replies)
Discussion started by: Naresh101
7 Replies

4. Shell Programming and Scripting

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 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

5. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

6. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

7. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

8. Shell Programming and Scripting

Combine two files and put it in .csv file

Hi Freinds I have two .txt file gem1.txt and gem2.txt, Sample gem1.txt abstract (1.0.0) actionmailer (2.3.5, 2.2.2) actionpack (2.3.5, 2.2.2) activerecord (2.3.5, 2.2.2) activerecord-oracle_enhanced-adapter (1.1.9) activerecord-sqlserver-adapter (2.3.4) activeresource (2.3.5, 2.2.2)... (3 Replies)
Discussion started by: ankit_view24
3 Replies

9. Shell Programming and Scripting

Put the difference of two files in out file

Hello, I have two files file1 & file2 containing both lines (1 word per line). I need to extract the lines that are in file1 and not present in file2 and have the result in output file. i.e : user>cat file1 line1 line2 line3 line4 line5 user>cat file2 line1 line3 line5 The... (2 Replies)
Discussion started by: newpromo
2 Replies

10. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies
Login or Register to Ask a Question