in reply to Re^4: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
in thread Need to have perl script to compare two txt files and print difference along with under which segment the difference is

Try

#!/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; }
poj
  • Comment on Re^5: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
  • Download Code

Replies are listed 'Best First'.
Re^6: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by User_04271983 (Initiate) on Jan 24, 2019 at 14:00 UTC
    Thank you so much poj!! It works.. You guys are best.. Thanks to every one who spent time on this..
Re^6: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by User_04271983 (Initiate) on Jan 24, 2019 at 14:32 UTC
    Hello poj.. Is it possible to have file name behind the segment name to identify from which file the record is from. output looks like below
    lodv EJHFG sdmd IJFH sdmd LAKJSK
    Thanks much for your efforts on this..
      } else { my $file = $file[0]; $file = $file[1] if defined $rec->[1]; printf "%s %s %s\n",$segment,$row,$file; }
      poj