User_04271983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Can someone help out with a perl script to compare two files and print the difference along with segment name under which the difference is

Requirement I have tst1.txt and tst2.txt tst1.txt have data as below Note: ABCD & EFGH are segments in below data ABCD TEST1 TEST2 TEST3 EFGH TEST1 TEST2 TEST3 tst2.txt have data as below ABCD TEST1 TEST2 TEST3 EFGH TEST1 TEST2 TEST3 TEST4 TEST5 Now diff between two files is in tst2.txt the last two rows "TEST4", " +TEST5" I need to have a script which gives output as below Diff between two files is from segment EFGH TEST4 TEST5

I am calling EFGH as segment, and it is must to have it in output file eventhough segment name is same from both files(ex: EFGH from above data). Also the the text files contains around 2 thousand to 3 thousand records. I need to find out difference along with under which segment(Name) the difference is. Pls help..

  • Comment on 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: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by tybalt89 (Monsignor) on Jan 24, 2019 at 17:41 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1228906 use strict; use warnings; @ARGV or @ARGV = qw( tst1 tst2 ); # FIXME for testing only @ARGV == 2 or die "usage: $0 filename1 filename2\n"; my %lines; for my $file ( @ARGV ) { my $segment; open my $fh, '<', $file or die "$! opening $file"; while( <$fh> ) { if( /^\w/ ) { $segment = $_; } elsif( $segment && /\w/ ) { $lines{"$segment$_" } .= "\t$file"; } } close $fh; } my $segment; for( sort grep $lines{$_} =~ tr/\t// == 1, keys %lines ) { if( $segment && /^\Q$segment\E(.+)/ ) { print "$1$lines{$_}\n"; } elsif( /^(.*\n)/ ) { $segment = $1; print s/(?=\n$)/$lines{$_}/r; } }

    Outputs:

    lodv EJHFG tst1 sdmd IJFH tst2 LAKJSK tst2
Re: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by hippo (Archbishop) on Jan 24, 2019 at 10:57 UTC
    Pls help..

    Which part are you having problems with, specifically? Show the code you have; an SSCCE would be best. If you have no code, show your algorithm. If you have no algorithm, create one.

      Hi Thanks for reply!!

        Without sight of the files you are comparing it is difficult to provide a solution. You mention segment names so is it safe to assume that each file contains the same segments but the contents of each segment may differ between files. If this is the case a better approach would be to break each file into segments and compare those, e.g. file test1 segment EFGH compared to file test2 segment EFGH rather than comparing the whole files. That way you can keep track of which segments differ.

        I hope my guess is close and this is helpful. Please post small example data files so that we can give better advice.

        Cheers,

        JohnGG

Re: Need to have perl script to compare two txt files and print difference along with under which segment the difference is
by thanos1983 (Parson) on Jan 24, 2019 at 16:05 UTC

    Hello User_04271983,

    Welcome to the Monastery. A very simple solution that will give you the desired output is:

    #!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines1 = io('one.txt')->chomp->slurp; my @lines2 = io('two.txt')->chomp->slurp; # print Dumper \@lines1, \@lines2; use Algorithm::Diff qw(diff); my @sdiffs = diff( \@lines1, \@lines2 ); print Dumper \@sdiffs; __END__ $ perl test.pl $VAR1 = [ [ [ '-', 13, ' EJHFG' ] ], [ [ '+', 22, ' IJFH' ], [ '+', 23, ' LAKJSK' ] ] ];

    It will not give you the key where there is a difference in the file but it is really nice and short as a solution to know the line number and go and check manually. Alternatively you can parse the output and create a loop and retrieve the lines from the array your self :)

    Hope this helps, Thanos.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Thanks Thanos!!