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.txt
Name Col1 Col2 Col3
-----------------------
row1 1 4 7
row2 2 5 8
row3 3 6 9
file2.txt
Name Col1 Col2 Col3
-----------------------
row2 1 4 11
row1 2 5 12
Here 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 AWK
- The rows may be out of order in the files
- No double arrays since my version of AWK doesn't support it
I 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.txt
How 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.
diff(1)
for, perhaps with a little post-massage bygrep(1)
to get rid of missing rows, and possibly preprocessing withsort(1)
for out-of-order lines...