comparing two files and find mismatch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing two files and find mismatch
# 1  
Old 02-15-2009
Network 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
43236|1|0|1|1|0|0|0|0|2
41404|1|0|1|1|0|0|0|1|2
79968|1|0|1|1|0|0|0|1|2

output should be like
file1:52424|1|0|1|1|0|0|0|1|2
file2:52424|1|0|1|1|0|0|0|0|2
-----------------
file1:43236|1|0|1|1|0|0|0|1|2
file2:43236|1|0|1|1|0|0|0|0|2Smilie
# 2  
Old 02-15-2009
Please use [code] tags when you post sample data.

Code:
awk 'NR==FNR{a[NR]=$0;next}{if (a[FNR] != $0)printf "%s\n%s\n\n",a[FNR], $0}' file1 file2 > output.file

Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk)
# 3  
Old 02-15-2009
it print all the lines while they are similar of diffirent

o/p comes.

00354|1|0|1|1|0|0|0|1|2|0|0|0|0|0|0|0|0|0|0|0
00354|1|0|1|1|0|0|0|1|2|0|0|0|0|0|0|0|0|0|0|0

52424|1|0|1|1|0|0|0|1|2|0|0|0|0|0|0|0|0|0|0|0
52424|1|0|1|1|0|0|0|3|2|0|0|0|0|0|0|0|0|0|0|0

43236|1|0|1|1|0|0|0|1|2|0|0|0|0|0|0|0|0|0|0|0
43236|1|0|1|1|0|0|0|1|2|0|0|0|0|0|0|0|0|0|0|0

but i want o/p should be only diff lines like red lines in my o/p file it does not show remaining similar lines
# 4  
Old 02-15-2009
What's the 'common key' between 2 files?
First field?
# 5  
Old 02-15-2009
Quote:
Originally Posted by dodasajan
it print all the lines while they are similar of diffirent
Let's see:
Code:
# cat 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
# cat file2
00354|1|0|1|1|0|0|0|1|2
52424|1|0|1|1|0|0|0|0|2
43236|1|0|1|1|0|0|0|0|2
41404|1|0|1|1|0|0|0|1|2
79968|1|0|1|1|0|0|0|1|2
# awk 'NR==FNR{a[NR]=$0;next}{if (a[FNR] != $0)printf "file1:%s\nfile2:%s\n\n",a[FNR], $0}' file1 file2
file1:52424|1|0|1|1|0|0|0|1|2
file2:52424|1|0|1|1|0|0|0|0|2

file1:43236|1|0|1|1|0|0|0|1|2
file2:43236|1|0|1|1|0|0|0|0|2

My code solve the OP request. If you need something else try to elaborate.
Quote:
Originally Posted by dodasajan
output should be like
file1:52424|1|0|1|1|0|0|0|1|2
file2:52424|1|0|1|1|0|0|0|0|2
-----------------
file1:43236|1|0|1|1|0|0|0|1|2
file2:43236|1|0|1|1|0|0|0|0|2Smilie
# 6  
Old 02-15-2009
yes first field is common in both the files

i want to compare on the basis of first field
# 7  
Old 02-15-2009
Code:
nawk -F'|' 'FNR==NR {a[$1]=$0; next} $0 != a[$1] { printf "%s\n%s\n\n",a[$1], $0}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find Syllable count mismatch

Hello, I have written a syllable splitter for Pseudo English and Indic. I have a large database with the following structure Syllables in Pseudo English delimited by |=Syllables in Devanagari delimited by | The tool produces syllables in both scripts. An example is given below: ... (2 Replies)
Discussion started by: gimley
2 Replies

2. 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

3. 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

4. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

5. 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

6. Shell Programming and Scripting

Script to find files and delete it by comparing

I have a directory where lot of "gzip" files are dropped in every 5 minutes. There is an application which will process these gzip and move it to another directory but will leave a gzip.out file with lot of output data. I need to remove all the outfiles except for the one which is being... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

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. ... (5 Replies)
Discussion started by: dgmm
5 Replies

8. Shell Programming and Scripting

To find String mismatch

Hi, I have a doubt when searching files for the existence of a particular key. I have a property file has data with key and value pair like below and i call it as property file.ini here are the contents in File: popertyfile.ini location.property=2 agent.method=begin newkey=23 ... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies

9. Shell Programming and Scripting

Find duplicate value comparing 2 files and create an output

I need a perl script which will create an output file after comparing two diff file in a directory path: /export/home/abc/file1 /export/home/abc/file2 File Format: <IP>TAB<DeviceName><TAB>DESCRIPTIONS file1: 10.1.2.1.3<tab>abc123def<tab>xyz.mm1.ppp.... (2 Replies)
Discussion started by: ricky007
2 Replies

10. UNIX for Dummies Questions & Answers

Help comparing 2 files to find deleted records

Hi, I need to compare todays file to yesterdays file to find deletes. I cannot use comm -23 file.old file.new. Because each record may have a small change in it but is not really a delete. I have two delimited files. the first field in each file is static. All other fields may change. I... (2 Replies)
Discussion started by: eja
2 Replies
Login or Register to Ask a Question