意外的结果在两个文本文件比较行的值和列 [英] Unexpected result comparing values of rows and columns in two text files
问题描述
这关系到我的previous问题之一:<一个href=\"http://stackoverflow.com/questions/24294876/how-to-compare-values-of-rows-and-columns-of-two-text-files-in-bash\">How到两个文本文件的行值和列比较在bash?
This is related to one of my previous question: How to compare values of rows and columns of two text files in bash?
FILE1.TXT(有特殊字符:猫-vet FILE1.TXT)
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(有特殊字符:猫-vet FILE2.TXT)
file2.txt (with special characters: cat -vet file2.txt)
Name Col1 Col2 Col3^M$
-----------------------^M$
row1 1 4 7^M$
row2 2 5 999$
我有所想出一个办法来比较两个文件。但由于某些原因,它不给我,我要的结果。裸陪我,因为我是一个初学者。这里是code我用比较的文件。
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
预期结果:
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
实际结果:
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
有谁知道为什么实际效果的线3和6 0(不应该这样做)?比较
Does anyone know why lines 3 and 6 of the actual result are comparing with 0 (not supposed to do so)?
**提前道歉跨职位,但我仍然有一个错误,我不能找到它。
** Apologies in advance for the cross post but I still have a bug and I can't find it
推荐答案
特别感谢@jaypal帮我解决这个问题。问题是,这些文本文件被在MS-DOS创建的,并且被转换为Unix格式,以便在Linux或Unix环境中被执行。所以从上面的问题执行code的段之前,我不得不使用这个命令: DOS2UNIX的FILE1.TXT
Special thanks to @jaypal for helping me solve the problem. The problem was that these text files were created in MS-DOS and had to be converted to Unix format in order to be executed in Linux or Unix environment. So before executing the segment of code from the question above, I had to use this command: dos2unix file1.txt
这篇关于意外的结果在两个文本文件比较行的值和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!