MaiTiano
MaiTiano

Reputation: 711

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.

Upvotes: 0

Views: 3111

Answers (4)

pixelbeat
pixelbeat

Reputation: 31718

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

Upvotes: 1

Bruno Brant
Bruno Brant

Reputation: 8564

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.

Upvotes: 3

Ivan Krechetov
Ivan Krechetov

Reputation: 19220

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

Upvotes: 4

Scottie T
Scottie T

Reputation: 12195

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?

Upvotes: 1

Related Questions