Sign in to follow this  
_Sigma

question about diff

Recommended Posts

I have, what appears to be, rather odd output from GNU diff
2304,2305c2305
<  0.2786E+03, 0.0000E+00, 0.0000E+00, 0.2786E+03, 0.1504E-04, 0.0000E+00, 0.1486E-04, 0.1809E-06, 0.0000E+00, 0.0000E+00, 0.0000E+00, 0.1000E+01,
<  0.2786E+03, 0.0000E+00, 0.0000E+00, 0.2786E+03, 0.1508E-04, 0.0000E+00, 0.1490E-04, 0.1808E-06, 0.0000E+00, 0.0000E+00, 0.0000E+00, 0.1000E+01,
---
>  0.2786E+03, 0.0000E+00, 0.0000E+00, 0.2786E+03, 0.1503E-04, 0.0000E+00, 0.1485E-04, 0.1808E-06, 0.0000E+00, 0.0000E+00, 0.0000E+00, 0.1000E+01,


Notice that it says lines 2304 to 2305 were changed to line 2305...which doesn't make sense. It should be an add/delete, not a change! Is there something I have missed about the diff output?

Share this post


Link to post
Share on other sites
It could be a delete of 2 lines, and an add of 1 line.

Or, it could be that the 2 lines got replaced by 1 new line.

Given that they are collocated, what is wrong with the change interpretation?

Share this post


Link to post
Share on other sites
The two files I'm comparing I know have exactly the same number of lines. Individual lines might be different, but there should never be a delete or addition. I have verified that the files being compared are indeed identical in structure, except at the line level, where two corresponding lines have different numeric values.

Is there a way change this behaviour?

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
Notice that it says lines 2304 to 2305 were changed to line 2305...which doesn't make sense. It should be an add/delete, not a change! Is there something I have missed about the diff output?


The new line is not identical to either of the old lines. For example, look at the fifth item on each line. 0.1504E-04 vs. 0.1508E-04 vs. 0.1503E-04.

Quote:
The two files I'm comparing I know have exactly the same number of lines. Individual lines might be different, but there should never be a delete or addition.


You seem to be contradicting yourself now.

Share this post


Link to post
Share on other sites
Oh, you mean that you want to compare the i-th line int the first file with the i-th line in the second file, and you know that both files have the same number of lines. I don't think diff can do that, but writing a program to do it is trivial.

EDIT: Try this (no error handling, sorry):
#!/usr/local/bin/perl

open (F1, $ARGV[0]);
open (F2, $ARGV[1]);
while ($l1=<F1>) {
$l2=<F2>;
++$line_number;
print "${line_number}c${line_number}\n<$l1---\n>$l2\n" unless $l1 eq $l2;
}
close(F1);
close(F2);



Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
Oh, you mean that you want to compare the i-th line int the first file with the i-th line in the second file, and you know that both files have the same number of lines. I don't think diff can do that, bit writing a program to do it is trivial.

yes, ith row of file A vs ith row of file B.

diff can't do that? Ah. yeah I'm writing some Perl script at the moment, I just wanted to to stick to a "known" utility! :)

Quote:
Quote:

The two files I'm comparing I know have exactly the same number of lines. Individual lines might be different, but there should never be a delete or addition.

You seem to be contradicting yourself now.


See above. I guess I didn't phrase it clearly. I now understand that problem.

Alright I appreciate the input, thanks guys!

Share this post


Link to post
Share on other sites
So diff is being too smart -- probably the lines after this match each other.

You happen to know that this isn't the case. The perl script works. I'd add code at the end it consume any left in <F2> as a sanity check (just a while ($l2=<F2>) { $l1 = "\n"; ++$line_number; print ... unless ...; } copy/paste block to keep it simple, or refactor into a function).

You could also do a cat --number, which guarantees that lines that aren't on the same line in the file don't match. This might be less than efficient. It would use a 'known' utility.

Depending on the shell, there might be a way to bind two cat processes' output to two temporary file handles, then feed them to diff.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this