in reply to Re^3: 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

Thanks for reply.. And you guessed it right. Both files will have same segments but the contents of each segment may differ between files. I am posting sample data for reference tst1
lodv OIDSCRIPT LCLABCG NMESCRIPT FRSJFGHT IT RCNGHSGD CURINR CRDUSWD OPWO7GNOxuXVog ODXCP ODXHC ODXIT APSN EJHFG sdmd DUUPPY MDPSJN PCINKSJ FXDEMAIJSKL1 FXCYYEJ EMCYOAK DLMDWJF IRRNKAJ
contents of tst2
lodv OIDSCRIPT LCLABCG NMESCRIPT FRSJFGHT IT RCNGHSGD CURINR CRDUSWD OPWO7GNOxuXVog ODXCP ODXHC ODXIT APSN sdmd DUUPPY MDPSJN PCINKSJ FXDEMAIJSKL1 FXCYYEJ EMCYOAK DLMDWJF IRRNKAJ IJFH LAKJSK
In tst1 under segment "lodv" we have an extra record at last line "EJHFG" Same way, in tst2 under segment "sdmd" we have extra records "IJFH", "LAKJSK" So can we have difference records along with segments. Hope this gives bit more clarity on my question. Thanks in advance:)
  • Comment on Re^4: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
  • Select or Download Code

Replies are listed 'Best First'.
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 (Abbot) on Jan 24, 2019 at 13:51 UTC

    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
      Thank you so much poj!! It works.. You guys are best.. Thanks to every one who spent time on this..
      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