The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare 2 files.. amon Shell Programming and Scripting 8 1 Week Ago 07:34 AM
Compare files kharen11 UNIX for Advanced & Expert Users 25 03-14-2007 01:35 AM
compare two txt files space13 Shell Programming and Scripting 8 09-22-2006 06:40 AM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 10:34 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 08:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-28-2008
Registered User
 

Join Date: Mar 2008
Posts: 29
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-29-2008
Registered User
 

Join Date: Feb 2008
Posts: 70
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
try this out

Hi ,

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

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

Thanks&Regards
Naree
Reply With Quote
  #3 (permalink)  
Old 03-29-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,205
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
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.
Reply With Quote
  #4 (permalink)  
Old 03-29-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,205
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Or even:

Code:
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
Reply With Quote
  #5 (permalink)  
Old 03-30-2008
Registered User
 

Join Date: Mar 2008
Posts: 29
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 !!
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 03:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102