Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tlug: Horizontal diff?



Frank Bennett (lists.tlug):
>I would like to identify the diffs between the two sets, but reg'lar diff
>will just turn up discrepancies line by line.  What I would like to do is
>get a more fine-grained return on differences *within* the line, so I can
>get a bead on how much has actually changed.

Sounds fun. Here's a fairly bad-but-correct way of doing it in Perl.
It has *no* smartness at all; if you have lines:
foo123456
foob123456
it will tell you that everything after foo differs. If you want to
avoid that, read up about the Wu-Mamber algorithm and prepare to waste
a good number of days implementing it for character-by-character matching. 
Here, though, you get what you pay for:

#!/usr/bin/perl -w
use strict;
open (ONE, $ARGV[0]) or die $!; open (TWO, $ARGV[1]) or die $!;
my $line=0;
while (1) {
	my ($one,$two) = (scalar <ONE>,scalar <TWO>);
	$line++;
	last unless defined $one and defined $two;
	if ($one ne $two) {
		print "Lines differ, line $line: "; # $. doesn't work?
		print " (differing length, this won't be pretty)" 
			if length $one != length $two;
		print "\n";
		my @example.com=split //,$one; my @example.com = split //,$two; my $p=0;
		LINE: while (@example.com) {
			my ($aline, $bline);
			my ($a, $b);
			$p++ while (($a = shift @example.com) eq ($b = shift @example.com) and @example.com and @example.com); 
			last unless @example.com and @example.com;
			print "At position $p:\n";
			while ($a ne $b and @example.com and @example.com) {
		    		$aline.=$a; $bline.=$b;
				$a=shift @example.com; $b=shift @example.com; ++$p;
			}
			print "$ARGV[0]: $aline\n$ARGV[1]: $bline\n\n";
		}
	}	
}

Given a file "one":
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafhfhfhfasdddddddddddaaaaaaaaaaaaaaaaaaaaaaas
bbbbbbbbbbbbbbbbbbbbbbbbbbbbfeefehfbbbbbbbbbbbbbbbbbbbbbasdasdadsbabdbfefbbb

And a file "two":
aaaaaasdddsdsaaaaaaaaaaaaaaaaaafhfhfhfasdddddvddddddaaaaaaaaaaaaaaaaaaaaaaas
bbbbbbbbbbbbbbbbbbbbbbbbbbbbfeefehfbbbbbbbbb  bbbbbbbbbbasdasdadsbabdbfefbbb

bastard:simon ~ % perl hdiff.pl one two
Lines differ, line 1:
At position 6:
one: aaaaaaa
two: sdddsds
At position 30:
one: afhfhfhfas
two: fhfhfhfasd
At position 43:
one: d
two: v

Lines differ, line 2:
At position 44:
one: bb
two:


-- 
"By golly, I'm beginning to think Linux really *is* the best thing since
sliced bread."
(By Vance Petree, Virginia Power)
--------------------------------------------------------------------
Next Nomikai Meeting: February 18 (Fri) 19:00 Tengu TokyoEkiMae
Next Technical Meeting:  March 11 (Sat) 13:00 Temple University Japan
* Topic: TBD
--------------------------------------------------------------------
more info: http://www.tlug.gr.jp        Sponsor: Global Online Japan


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links