Edited: compare two files and print mismatch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edited: compare two files and print mismatch
# 1  
Old 06-21-2009
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 1 of file 2. Rather, the comparison is on line 1 of file 1 and the line on file 2 that has the same first word on the line 1 of file 1.


To demonstrate:
FILE 1:
abc 123 678
def 456 901
ghi 789 234
jkl 012 567
mno 345 890
FILE 2:
def 456 901
abc 124 678
mno 345 890
ghi 789 244
OUTPUT FILE:
"from file 1"
abc 123 678
ghi 789 234
"from file 2"
abc 124 678
ghi 789 244

i hope someone can help me with this. Thanks!
# 2  
Old 06-21-2009
Try this..

Code:
awk '{ if (FNR==NR) {arr[$1]=$0;next}
if (($1 in arr) && ($0!=arr[$1])) { f1[$1]=arr[$1]; f2[$1]=$0; next} } 
END { print "from file 1";  for (i in f1) {print f1[i]}; print "from file 2"; for (i in f2) {print f2[i]}  } ' file1 file2 > file3

Assumption: First word is unique in a given file. please let me know if you need to handle duplicates also so that I can try for that.
# 3  
Old 06-23-2009
hi king! the code you gave me doesn't work..
it doesn't print the lines with difference though i am sure that there are lines that have mismatch on the two files.

Thanks!
# 4  
Old 06-23-2009
What have you tried?
# 5  
Old 06-23-2009
i tried diff command But i learned that in diff command, it compares line by line.
while in cat file1 file2 | sort | uniq -u > file 3, it yileds:

ABC 123
ABC 321
DEF 412
DEF 124

and when i used it on my script, it yields a odd number of lines.
# 6  
Old 06-23-2009
Is this what you want...
Code:
sort file1 > file1_tmp
sort file2 > file2_tmp
sdiff file1_tmp file2_tmp | grep '|'

output:
Code:
abc 123 678                                                     |  abc 124 678
ghi 789 234                                                     |  ghi 789 244

# 7  
Old 06-23-2009
output should be:

"FROM FILE 1"
ABC 123
ABC 321


"FROM FILE2"
DEF 412
DEF 124
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
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

Compare two files and print list

Hi Gents, I have 2 files as seen below. File 1: 9 1020 10 1001 11 1001 12 1002 13 1003 14 1004 15 1004 File 2: 9 1000 11 1001 12 1002 13 1003 15 1004 (5 Replies)
Discussion started by: jiam912
5 Replies

4. Shell Programming and Scripting

Compare two files and print using awk

I have 2 files: email_1.out 1 abc@yahoo.com 2 abc_1@yahoo.com 3 abc_2@yahoo.com data_1.out <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td... (1 Reply)
Discussion started by: sol_nov
1 Replies

5. Shell Programming and Scripting

Compare columns 2 files and print

File 1 has 16 columns so does File 2 I want to remove all records from File 2 that column 1 and column 16 match between file 1 and file 2 delimter of files is ~ (10 Replies)
Discussion started by: sigh2010
10 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. 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

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

9. Shell Programming and Scripting

compare two files and print the last row into first

suppose fileA vis vis gyh gye gyh fileB vis 23 gyh 21 gye 32 output shud be like in fileA ... vis 23 vis 23 gyh 21 gye 32 gyh 21 (1 Reply)
Discussion started by: cdfd123
1 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question