我是这个论坛的新人,我有两个需要比较的文件,这些文件没有分隔符,并且数据按位置存在,因此需要逐行与每个字符进行比较。我使用了下面的 sdiff 命令,但它没有向我显示预期的输出,它仅显示不同的行号,但不显示字符的位置。
sdiff file1 file2 | grep "|" > log file
我尝试了下面的代码,你能帮忙吗?
for i in `cat /home/okanung/test_handoff/testfile1`
do
for j in `cat /home/okanung/test_handoff/testfile2`
do
for ((k=0;k <=`echo $i | wc -c `;k++))
do
a=${i:k:1}
b=${j:k:1}
if [ "$a" != "$b" ]
then
lineno=`grep -n "$i" /home/okanung/test_handoff/testfile1 | cut -d ":" -f 1`
echo "mismatch is at line no $lineno at position $k" | sort -n | uniq -c >> /home/okanung/test_handoff/test_log_file
fi
done
done
done
文件格式:06001234567800000009 20160226100TD22000002 04.00000.0000000000000030.000000000000028.800000 0000000000001.200000 000000 0000000.4200000000000000000.4000000000000000000.02000000000 000000000000000000000.4400000000000000000.0200000 000000000000.000000标准 0 221-通过率 201603075 10UW0000054321AB00000361593804INR20160226EUR001000001000000000000030.0000000000 0000000.0004.00004.00000000000000
答案1
使用cmp
:
$ cat file1.txt
This file
is the same
as the other
$ cat file2.txt
This file
is almost the same
as the other
$ cmp file1.txt file2.txt
file1.txt file2.txt differ: char 14, line 2
char 14
指第一个字符在文件中这是不同的。
对于单词差异的直观表示,请使用wdiff
:
$ wdiff file1.txt file2.txt
This file
is {+almost+} the same
as the other
答案2
您可以使用该diff
命令查看每个文件中哪些行不同。
diff file1.txt file2.txt
这将为您提供不同的行,在本例中:
文件1.txt:
"same line,
same line,
different line,
same line."
文件2.txt:
"same line,
same line,
same line,
same line."
$> diff file1.txt file2.txt
< line 3 : different line,
----------------------
> line 3 : same line,