The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
gawk and gensub ripat Shell Programming and Scripting 2 05-10-2008 12:20 PM
awk,gawk in bat file andrej Shell Programming and Scripting 0 05-09-2008 05:45 AM
gawk and strftime() ripat Shell Programming and Scripting 2 05-07-2008 11:30 AM
gawk will work or not ? tkbharani Shell Programming and Scripting 3 03-13-2007 03:26 PM
rs and ors in gawk ...???? moxxx68 Shell Programming and Scripting 2 10-05-2004 09:52 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
gawk HELP

I have to compare records in two files. It can be done using gawk/awk but i am unable to do it. Please help me


File1

ABAAAAAB BC asa sa
ABAAABAA BC bsa sm
ABBBBAAA BC bxz sa
ABAAABAB BC csa sa
ABAAAAAA BC dsa sm
ABBBBAAB BC dxz sa

File 2
ABAAAAAB BC aas ba
ABAAAAAB BC asa sa
ABAAABAA BC ban sm
ABBBBAAA BC bxz sa
ABAAABAB BC csa sa
ABAAAAAA BC dsa sm
ABBBBAAB BC dxz sa
ABBBBAAB BC fxz sa


How should files be compared
1. We should take 3rd and 4th fields(eg asa sa in file 1 line 1) in first file and look for it in second file's third and fourth columns.
2. If 3rd and 4th filed of first file matches with 3rd and 4th field of some record of 2nd file then we need to compare rest of line..like comapring ABAAAAAB BC in file 1 with ABAAAAAB BC in file 2 for asa sa.
3.If there is a mismatch then we need to give the output that mismatch has occured else No Error.

Please help in this regard

-Sandeep
Reply With Quote
Forum Sponsor
  #2  
Old 06-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
nawk -f sandeep.awk file2 file1

sandep.awk:
Code:
FNR==NR {arr[$3 FS $4] = $1 FS $2; next}
{
   if ( !($3 FS $4) in arr )
      print "MEGAmismatch"
   else if ( ($1 FS $2) != (arr[$3 FS $4]) )
      print "mismatch"
}
The rest is left up to the OP to figure out - not tested.
Reply With Quote
  #3  
Old 06-15-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
It is not printing MEGAmismatch at all some problem is there.
Reply With Quote
  #4  
Old 06-15-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
Quote:
Originally Posted by sandeep_hi
It is not printing MEGAmismatch at all some problem is there.
sorry - as I said - 'not tested':
Code:
FNR==NR {arr[$3 FS $4] = $1 FS $2; next}
{
   if ( !( ($3 FS $4) in arr) )
      printf("[%s]: MEGAmismatch of [%s]\n", FNR, $3 OFS $4)
   else if ( ($1 FS $2) != (arr[$3 FS $4]) )
      printf("[%s]: mismatch of [%s] on MEGAmatched [%s]\n", FNR, $1 OFS $2, $3 OFS $4)
}

Last edited by vgersh99; 06-15-2006 at 07:37 AM.
Reply With Quote
  #5  
Old 06-16-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
Thnaks alot !!!

I could not understand this line of code can u please explain me what exactly it is meant for and how is it happening:

Quote:
FNR==NR {arr[$3 FS $4] = $1 FS $2; next}
Reply With Quote
  #6  
Old 06-16-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
Quote:
Originally Posted by sandeep_hi
Thnaks alot !!!

I could not understand this line of code can u please explain me what exactly it is meant for and how is it happening:
Code:
FNR==NR {arr[$3 FS $4] = $1 FS $2; next}
Quote:
Originally Posted by man nawk
NR ordinal number of the current record

FNR ordinal number of the current record in the current file
'FNR==NR' will be true for the FIRST file to be processed

'arr' is an associative array indexed by the values of fields '3' and '4' and having the content of cancatenated value of fields '1' and '2'.

When processing the FIRST file specified on the command line we're building the hash/associative array used later on in the script for doing the 'lookups'.

Look into 'man nawk' for the details on the associative arrays.
Reply With Quote
  #7  
Old 06-19-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
Thank you so much .

Regards
sandeep
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:40 PM.


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

Content Relevant URLs by vBSEO 3.2.0