开发者

A shell script problem for comparing difference between two files. [Linux]

Now my program generates two data files. a.txt and b.txt Take a.txt as an exam开发者_JAVA技巧ple, it's content just like this:

0,0
0,1
1,0
-3,1
1,-2
1,3
......

b.txt is similar with a.txt.

Now, I hope to find out difference lines count. In other words, for example, if b.txt like this:

0,0
1,1
1,2
-3,1
1,-2
1,3
......

a shell script output 2 as the 2nd and the 3rd lines are different with one number different. How to do this???

I try diff command, however, I cannot get what I want...

Need your kind help..Thanks.

Addition: There are about 10,000 - 100,000 rows for each files. Of course, they have same no. of rows at each time.


diff a.txt b.txt | grep "<" | wc -l


Faced the same problem a while back. What you need is diffstat. Diffstat is part of the GNU diff package and can summarizes diff results:

SYNOPSIS

diffstat reads the output of diff and displays a histogram of the insertions, deletions, and modifications per-file. It is useful for reviewing large, complex patch files.

You can also process the output of diffstat to get summarized results:

diff -u FileA.txt FileB.txt | diffstat -f0 | grep -v files | awk '{ print $3 }'

Where -u is obligatory. You can explore diffstat documentation for options.


diff seems to be exactly what you want.

#> diff a.txt b.txt
2,3c2,3
< 0,1
< 1,0
---
> 1,1
> 1,2

Is there something more specific you were looking for?


diff may move chunks within a file which is not what you want I think. Here's an alternative:

join -t'\0' -v2 <(cat -n a.txt) <(cat -n b.txt) | wc -l
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜