Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is related to one of my previous question: Comparing text files using Bash and AWK

file1.txt

Name  Col1  Col2  Col3  
-----------------------
row1  1     4     7        
row2  2     5     8         
row3  3     6     9 

file2.txt

Name  Col1  Col2  Col3  
-----------------------        
row1  1     4     7
row2  2     5     999

I have somewhat figured out a way to compare two files. But for some reason it is not giving me the result that I want. Bare with me as I am a beginner. Here is the code I use to compare the files.

awk '
FNR < 2 { next }
FNR == NR {
    for (i = 2; i <= NF; i++) 
    {
        a[i,$1] = $i; 
    }    
    b[$1];
    next;

}
($1 in b) {
   for (i = 2; i <= NF; i++) 
   {
      if (a[i,$1] == $i) 
          printf("%s->Col%d: %d vs %d: Equal\n", $1, i-1, a[i,$1], $i);
      else 
          printf("%s->Col%d: %d vs %d: Not Equal\n", $1, i-1, a[i,$1], $i);
   }
} ' file1.txt file2.txt

Expected result:

row2->Col1: 1 vs 1: Equal
row2->Col2: 4 vs 4: Equal
row2->Col3: 7 vs 7: Equal
row1->Col1: 2 vs 2: Equal
row1->Col2: 5 vs 5: Equal
row1->Col3: 8 vs 999: Not Equal

Actual result:

row2->Col1: 1 vs 1: Equal
row2->Col2: 4 vs 4: Equal
row2->Col3: 0 vs 7: Not Equal
row1->Col1: 2 vs 2: Equal
row1->Col2: 5 vs 5: Equal
row1->Col3: 0 vs 999: Not Equal

I don't know why 0 is part of the output. Does anyone know why 0 is there?

EDIT: Here is the problem. The first for loop only iterates once. Does anyone know why?

share|improve this question

closed as off-topic by jasonwryan, Ramesh, slm, vonbrand, derobert Jun 20 at 7:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – jasonwryan, Ramesh, slm, vonbrand, derobert

    
The b array contains only 0. I'm not very good with awk, what does ($1 in b) do? Goes through all b array elements? –  polym Jun 19 at 19:06
    
@polym Yes exactly –  Alias Jun 19 at 19:08
    
did you forget to assign any values to these array elements? you only created them in line 6 of your awk expression b[$1];, which results in 0s –  polym Jun 19 at 19:14
    
@polym I created the b[$1]; array because I need it as reference for comparing the second file. Check the actual output (lines 1 and 4). There is an actual comparison going on there so assigning a value to b[$1] is not the issue. See the EDIT in my answer. The problem is something with the first for loop –  Alias Jun 19 at 19:25
1  
Please stop crossposting. –  jasonwryan Jun 19 at 21:33

1 Answer 1

Bug: change

FNR == NR {
    for (i = 2; i <= NF; i++) 
    {
        a[i,$1] = $i; 
        b[$1];
        next;
    }
}

to

FNR == NR {
    for (i = 2; i <= NF; i++) 
    {
        a[i,$1] = $i; 
    }
    b[$1];
    next;
}

You were calling next after i==2

share|improve this answer
    
@glenn_jackman Thanks! But that only solved part of the problem. Now the output looks my this (see edit in the question) –  Alias Jun 19 at 19:33
    
@Nosscire: My mistypo in original question. After glenn jackman fixed, it now works. –  cuonglm Jun 19 at 19:57
    
@Gnouc I updated the question with the error I am still getting. Check lines 3 and 6 of the Actual Output in the question. Still have no idea as to why it is not happening –  Alias Jun 19 at 20:35
    
It works perfect for me. You can add a print after every a[i,$1] = $i. –  cuonglm Jun 19 at 20:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.