Gossamer Forum
Home : General : Perl Programming :

exact comparison of two text files.

Quote Reply
exact comparison of two text files.
File One:
---------------------
This is a text file containing a couple of lines.
and will be edited many times.
---------------------

When File One is edited, it will write the File Two, given below:

File Two:
---------------------
This is a text file containing a couple of lines.
and will NEW WORDS ADDED HERE be edited many times.
---------------------

Now I want to compare both File1 and File2 to write a File3 in
which 'NEW WORDS ADDED HERE' or any other changes could be
highlighted (for example surrounded by bold <b> tags of HTML).

any ideas?

Thanks in advance.

Zeshan.

Quote Reply
Re: [zeshan] exact comparison of two text files. In reply to
A possible way to do it, although I doubt its the fastest method;

Code:

my @data;
open (READORIG,"original.txt") || die "Cant open. Reason: $!";
@data = <READORIG>;
close(READORIG);

my @data_copy;
open (READCOPY,"copy.txt") || die "Cant open. Reason: $!";
@data_copy = <READCOPY>;
close(READCOPY);

my $data = join("",@data);
my $data_copy = join("",@data_copy);

# get rid of original message from $data, so we
# are just left with the changes that have been added onto the end (hopefully)
$data_copy =~ s/\Q$data_copy/$data/si;

print "Content-type: text/html \n\n";
print "Extra added to file: \n\n <BR><BR>";
print $data_copy;

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] exact comparison of two text files. In reply to
Andy, you are marvelous and thanks for your help.

But I am still stuck. I don't want to rip off the changes I want the changes to be highlighted within the original text. I don't want to get rid of original message from the $date rather highlighting the changes in it like i.e. new added text surrounded by <b></b> HTML tags.

In the meantime I looked at CPAN
http://www.perldoc.com/perl5.6/lib/File/Compare.html

File::Compare, compare_text was an option but it returns true and false only and my purpose is not served, do you know of anyother similar things that can serve my purpose or can help me further.

Once again thanks for helping newbies like me.

Zeshan.






Quote Reply
Re: [zeshan] exact comparison of two text files. In reply to
If I understood you correctly, you are looking for Text::Diff and Algorithm::Diff libraries. Algorithm::Diff implements the algorithm, and Text::Diff uses it to achieve the result you're trying to.