awk - Unexpected result comparing values of rows and columns in two text files -
this related 1 of previous question: how compare values of rows , columns of 2 text files in bash?
file1.txt (with special characters: cat -vet file1.txt)
name col1 col2 col3^m$ -----------------------^m$ row1 1 4 7^m$ row2 2 5 8^m$ row3 3 6 9$
file2.txt (with special characters: cat -vet file2.txt)
name col1 col2 col3^m$ -----------------------^m$ row1 1 4 7^m$ row2 2 5 999$
i have figured out way compare 2 files. reason not giving me result want. bare me beginner. here code utilize compare files.
awk ' fnr < 2 { next } fnr == nr { (i = 2; <= nf; i++) { a[i,$1] = $i; } b[$1]; next; } ($1 in b) { (i = 2; <= 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
does know why lines 3 , 6 of actual result comparing 0 (not supposed so)?
** apologies in advance cross post still have bug , can't find it
special @jaypal helping me solve problem. problem these text files created in ms-dos , had converted unix format in order executed in linux or unix environment. before executing segment of code question above, had utilize command: dos2unix file1.txt
awk scripting comparison
No comments:
Post a Comment