I have two text files, and I want to compare their corresponding values according to their rows and columns. By comparing, I mean to check if the values are equal and echo if the values are the same or not. Here are the files:file1.txtName  Col1  Col2  Col3  -----------------------row1  1     4     7        row2  2     5     8         row3  3     6     9   file2.txtName  Col1  Col2  Col3  -----------------------row2  1     4     11        row1  2     5     12Here are the constraints:compare only the rows that exist (that is, since row3 exists in file1.txt, but not in file2.txt, no comparison is needed)Must use AWKThe rows may be out of order in the filesNo double arrays since my version of AWK doesn't support itI am thinking of something like this:awk 'NR>2 {next}{    for (i=2;i<NR;i++)              #For each row of file1.txt    {             for(j=1;i<NF;j++)           #For each column of file1.txt        {             // Check if row and column of file1.txt is equal to row and column of file2.txt.        }     }}' file1.txt file2.txtHow do you compare values of two different text files? Let me know if more explanation is needed.All I'm asking for is a general structure using AWK. Hopefully I can figure out the rest.
Comparing text files using Bash and AWK
scripting;awk;diff
There are many ways to do it, here's a solution with gawk 4.x:$ awk '    FNR < 2 { next }    FNR == NR {        for (i = 2; i <= NF; i++) {            a[$1][i] = $i;        }        next;    }    ($1 in a) {      for (i = 2; i <= NF; i++) {          if (a[$1][i] == $i) {              printf(%s->Col%d: Equal\n, $1, i-1);          } else {              printf(%s->Col%d: Not Equal\n, $1, i-1);          }       }  }' file1.txt file2.txtrow2->Col1: Not Equalrow2->Col2: Not Equalrow2->Col3: Not Equalrow1->Col1: Not Equalrow1->Col2: Not Equalrow1->Col3: Not EqualExplanationFNR < 2 { next }: skip first two linesFNR == NR: only true when we processing first file. We save each column value in associative array a, with form a[ROW][COLUMN].($1 in a): check if row in file2 existed in file1. If true, we loop through all its column value, compare with value in file1.With older gawk version, you can try:$ 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: Equal\n, $1, i-1);          } else {              printf(%s->Col%d: Not Equal\n, $1, i-1);          }       }  }' file1.txt file2.txt