Try
poj#!/usr/bin/perl use strict; use warnings; my @file = ('tst1.txt','tst2.txt'); my %compare = (); # inputs for my $n (0..$#file){ parse_file($n); } # output diff for my $segment (sort keys %compare){ for my $row (sort keys %{$compare{$segment}}){ my $rec = $compare{$segment}{$row}; if (defined $rec->[0] && defined $rec->[1]){ # matched } else { printf "%s %s\n",$segment,$row; } } } sub parse_file { my ($n) = @_; my $filename = $file[$n]; my $segment; open IN,'<', $filename or die "Could not open $filename : $!"; while (<IN>){ s/\s+$//; # trim trailing whitespace if (s/^\s+//){ ++$compare{$segment}{$_}[$n]; } else { $segment = $_; } } close IN; }
In reply to Re^5: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by poj
in thread Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by User_04271983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |