简体   繁体   中英

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 example, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM