compare two files

File1
E100 0 5/29/1993 0 E001 E000
E102 0 1/23/1994 0 E001 E003
E104 0 6/4/1994 0 E001 E003
E105 0 7/30/1993 0 E001 E003
E106 0 1/9/1993 0 E001 E003
E108 0 2/3/1995 0 E001 E003
E109 0 2/15/1995 0 E001 E001

File2
E108 0 2/3/1995 0 E001 E003
E109 0 2/15/1995 0 E001 E001
E110 0 2/15/1995 0 E001 E001
E111 0 9/15/1996 0 E001 E001
E112 0 4/21/1997 0 E001 E001

These are the two files
Column 1,2 and 3 are the key fields.
A TO B MAPPING

I need to compare file1 key columns and the file2 key columns. If File1 key column matches with File2 key column move the entire row of file1 which are matching to > File3.

E108 0 2/3/1995 0 E001 E003
E109 0 2/15/1995 0 E001 E001

If the key columns of file1 not matches with file2 then move the entire row of the file1 which are not matching in the file2 > File4.

E100 0 5/29/1993 0 E001 E000
E102 0 1/23/1994 0 E001 E003
E104 0 6/4/1994 0 E001 E003
E105 0 7/30/1993 0 E001 E003
E106 0 1/9/1993 0 E001 E003

///LY

B TO A mapping

To file5 and file6.

Please let me know on this ASAP.........

Thanks in advance

CHARAN

Hi ,

diff -U 2 file1 file2 | awk '/^\ / {print $0}' >> file3

diff -U 2 file1 file2 | awk '/^-/ {print $0}' >> file4

Thanks&Regards
Naree

awk 'END { for (k in f2) {
    split(f2[k], t)
    print f2[k] > (((t[1] SUBSEP t[2] SUBSEP t[3]) in f1) ? "file5" : "file6")
    }
}
NR == FNR { 
f2[$1,$2,$3] = $0
next 
}
($1 SUBSEP $2 SUBSEP $3) in f2 { 
print > "file3" 
f1[$1,$2,$3] = $0 
next 
}
{ print > "file4" }
' file2 file1 

Use nawk or /usr/xpg4/bin/awk on Solaris.

Or even:

awk 'END { for (k in f2) {
    split(f2[k], t)
    print f2[k] > (((t[1] SUBSEP t[2] SUBSEP t[3]) in f1) ? "file5" : "file6")
    }
}
NR == FNR { f2[$1,$2,$3] = $0; next }
{ print > (($1 SUBSEP $2 SUBSEP $3) in f2 ? "file3" : "file4")  
f1[$1,$2,$3] = $0 }' file2 file1 

Thanks !

It is working when we have 3 key fields.

II Step :

I want to pass key fields dynamically .....( file may have key fields upto 4,5,6 etc. ) then what changes i have to do for the above .....

Can you plz help me !!

Can you please explain me the code ......

Could you post an example of how exactly you would like to pass the parameters? What format?

Using the same syntax as cut would seem fairly natural. Or if you want to make it really tricky, join (but then why not use join itself in the first place. Of course, if you don't have to sort the input, that's a win.)