Compare two files and mismatch report


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and mismatch report
# 1  
Old 05-24-2010
Compare two files and mismatch report

Hi
I have two files f1 and f2 and comma separated file.
I need to comapre two files by field by field and not by whole line.
If they match then skip the line from both the files. If they don't match
write the mismatch record from f1 to f3.
Assume both the files are sorted on first field.

Code:
f1
a1,a2,a3,a4,a5,a6
b1,b21,b31,b4,b5,b5
c1,c2,c3,c4,c5,c6
 
f2
a1,a2,a3,a4,a5,a6
b1,b2,b3,b4,b5,b6
c1,c2,c3,c4,c51,c61
d1,d2,d3,d4,d5,d6
 
The Output f3
b1,b21,b31,b4,b5,b6
c1,c2,c3,c4,c5,c6

I don't want to compare line by line , this may be required to tuke the data in some field.
I am just looking for Perl or unix script, how should I proceed about this.

Thanks
Dgmm
# 2  
Old 05-24-2010
Code:
grep -vf f2 f1

gives the result you show in the example
# 3  
Old 05-24-2010
Hi
I want to compare for each line and each field of that line.
Read the first line
split the line to different fields.
Compare
f1.first field with f2.first field
f1.second field with f2.second field
and so on
if any of the field mismatch then
write the f1 record to f3.

Complete the loop till end of file f1.

Can some once interpret this in Perl or awk.
This is what I want.
By the way the grep command didn't work in my shell.

Thanks
Dgmm
# 4  
Old 05-24-2010
Hi, dgmm.
Quote:
Originally Posted by dgmm
Hi
I have two files f1 and f2 and comma separated file.
I need to comapre two files by field by field and not by whole line.
If they match then skip the line from both the files. If they don't match
write the mismatch record from f1 to f3.
Assume both the files are sorted on first field.

Code:
f1
a1,a2,a3,a4,a5,a6
b1,b21,b31,b4,b5,b5
c1,c2,c3,c4,c5,c6
 
f2
a1,a2,a3,a4,a5,a6
b1,b2,b3,b4,b5,b6
c1,c2,c3,c4,c51,c61
d1,d2,d3,d4,d5,d6
 
The Output f3
b1,b21,b31,b4,b5,b6
c1,c2,c3,c4,c5,c6

I don't want to compare line by line , this may be required to tuke the data in some field.
I am just looking for Perl or unix script, how should I proceed about this.

Thanks
Dgmm
Welcome to the forum.

I could not get the grep to work, either.

On the other hand, I don't see where this line in f3 comes from:
Code:
b1,b21,b31,b4,b5,b6

Is that a typo? ... cheers, drl
# 5  
Old 05-24-2010
Quote:
Originally Posted by dgmm
Hi
I have two files f1 and f2 and comma separated file.
I need to comapre two files by field by field and not by whole line.
If they match then skip the line from both the files. If they don't match
write the mismatch record from f1 to f3.
Assume both the files are sorted on first field.

I don't want to compare line by line , this may be required to tuke the data in some field.
I am just looking for Perl or unix script, how should I proceed about this.

Quote:
Originally Posted by dgmm
Hi
I want to compare for each line and each field of that line.
Read the first line
split the line to different fields.
Compare
f1.first field with f2.first field
f1.second field with f2.second field
and so on
if any of the field mismatch then
write the f1 record to f3.

Complete the loop till end of file f1.

I don't understand why you say that you don't want to compare line by line. The steps which you listed will only yield matching records if all fields are identical, which means the lines are identical. If any fields differ, the lines will differ.

Or is there something you neglected to mention about the data in the fields? For example, is whitespace insignificant? Are the fields actually numbers and leading zeroes should not affect a numeric comparison? Is there something that could lead to unidentical lines matching when handled on a field by field basis?

Regards,
Alister
# 6  
Old 05-25-2010
hi alister/drl
You have understood correctly why I am not interested need to compare whole line.

I mis typed the input field.
Yes there might be a field which numeric
so I will compare by lpadding at that time.
So I don't want to compare whole line.

Code:
f1
a1,a2,a3,a4,a5,a6
b1,b21,b31,b4,b5,b6
c1,c2,c3,c4,c5,c6
 
f2
a1,a2,a3,a4,a5,a6
b1,b2,b3,b4,b5,b6
c1,c2,c3,c4,c51,c61
d1,d2,d3,d4,d5,d6
 
The Output f3
b1,b21,b31,b4,b5,b6
c1,c2,c3,c4,c5,c6

Please let me know how to read each line , parse it and compare till end of file.


Thanks
Dgmm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Compare two files and show the mismatch columns

I need to compare two files and find the mismatch columns in it for csv and fixed width file. Eg: file1 c1,c2,c3,c4<----columnname 1,a,4,d 2,b,5,e 3,c,6,f file2 c1,c2,c3,c4<----columnname 3,x,7,f 2,y,8,e 1,z,9,d output c2,c3<---- mismatch columname a,4 x,7 b,5 or y,8 Ok with... (3 Replies)
Discussion started by: sabzR
3 Replies

2. Shell Programming and Scripting

Need help in comparing two files in UNIX with a mismatch

Hi Everyone, I am comparing results of two environments using unix files. I am writing two different csv file using spool and merging both the files using paste command paste -d, file1.csv file2.csv > prod_uat_result.csv and then finding the difference and attaching the same in a mail... (8 Replies)
Discussion started by: karthik adiga
8 Replies

3. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

4. UNIX for Dummies Questions & Answers

Files count mismatch when used with Tar with find

Hi I have used the below steps and found some discrepancies step 1 : find ./ -type f -mtime +7 -name "*.00*" | wc -l = 13519 ( total files ) ( the size of this files is appx : 10GB ) step 2: find ./ -type f -mtime +7 -name "*.00*" | xargs tar zcvf Archieve_7.tar.gz step... (7 Replies)
Discussion started by: rakeshkumar
7 Replies

5. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

6. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

7. UNIX for Dummies Questions & Answers

mawk script to compare 2 files and report where they match

I have two files and would like a report of where they match. Example of file1: 1 1 1 2 2 2 13 14 15 4 4 4 15 16 17 100 102 1004 56 57 890 Example of file2: 2 2 2 16 10 11 45 22 35 13 14 15 1001 1002 3456 100 102 1004 (1 Reply)
Discussion started by: kenneth.mcbride
1 Replies

8. Shell Programming and Scripting

Edited: compare two files and print mismatch

Using unix shell script, how to compare two files and print lines with mismatch? Below are the requirements: 1. The number of lines on the two files is not the same. 2. The difference/mismatch can be found on the second or third column. 3. The comparison is not between line 1 of file 1 and line... (16 Replies)
Discussion started by: kingpeejay
16 Replies

9. Shell Programming and Scripting

compare three files and insert a blank line at each mismatch

i need to compare three files in unix a.txt b.txt c.txt 1 2 1 2 5 3 4 6 5 5 6 6 i need to insert a blank line in the file if i don't find a match and put the items at the same column if found a match The items in the files... (4 Replies)
Discussion started by: mad_man12
4 Replies

10. Shell Programming and Scripting

comparing two files and find mismatch

hi i have two files and i want to compare both the files and find out mismatch in 3rd file file1 00354|1|0|1|1|0|0|0|1|2 52424|1|0|1|1|0|0|0|1|2 43236|1|0|1|1|0|0|0|1|2 41404|1|0|1|1|0|0|0|1|2 79968|1|0|1|1|0|0|0|1|2 file2 00354|1|0|1|1|0|0|0|1|2 52424|1|0|1|1|0|0|0|0|2... (9 Replies)
Discussion started by: dodasajan
9 Replies
Login or Register to Ask a Question