Here's an example of Algorithm::Diff. Since you didn't show what the output should look like, I am only printing to STDOUT. Items only in the first file are shown in red, and items only in the second file are shown in green, where "item" is either a line, a word (non-whitespace), or a character based on the switch provided.

Note that most of the code is just handling options.

#!/usr/bin/perl # https://perlmonks.org/?node_id=1228795 use strict; # color diff use warnings; use Algorithm::Diff qw(traverse_sequences); use Term::ANSIColor; use Getopt::Long; GetOptions 'lines' => \(my $lines), 'chars' => \(my $chars), 'words' => \(my $words), 'help' => \(my $help), or die help("bad option"); sub help { print <<END; @_ --lines diff by lines (default) --chars diff by characters --words diff by words (non-whitespace) --help help END exit; } $help and help("options"); @ARGV == 2 or die "usage: $0 -h -l -w -c oldfilename newfilename\n"; my $regex = $chars ? qr/./s : $words ? qr/\S+|\h+|\n/ : qr/.*/s; my @from = do { local @ARGV = shift; map /$regex/g, <> }; my @to = do { local @ARGV = shift; map /$regex/g, <> }; traverse_sequences( \@from, \@to, { MATCH => sub {print $from[shift()]}, DISCARD_A => sub {print color('red'), $from[shift()], color 'reset'} +, DISCARD_B => sub {print color('green'), $to[pop()], color 'reset'}, } );

In reply to Re: comparing any two text files and writing the difference to a third file by tybalt89
in thread comparing any two text files and writing the difference to a third file by balanunni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.