![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | 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. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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"
}
|
|
#3
|
|||
|
|||
|
It is not printing MEGAmismatch at all some problem is there.
|
|
#4
|
||||
|
||||
|
Quote:
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. |
|
#5
|
|||
|
|||
|
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:
|
|
#6
|
||||
|
||||
|
Quote:
Code:
FNR==NR {arr[$3 FS $4] = $1 FS $2; next}
Quote:
'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. |
|
#7
|
|||
|
|||
|
Thank you so much .
Regards sandeep |
|||
| Google The UNIX and Linux Forums |