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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |