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

I am trying to make script in perl which compare two files (format doesn't matter), both files have few similar data but whatever diff between two should be print out error.txt.

Suppose file1 and file2 are files which has to compare but the logic is compare file2 over file1 and whatever changes in file2 should be printed as "Error: lineno:(no. at the point of mismatch) please check error" in error.txt.

file1: Figure 1. Somatotropes are organized into. Figure 2. Comparing two xml files organized into. Figure 3. Somatotropes presentation of GH1, file2: Figure 1. children with acquired organized into. Figure 2. Severe anterior hypoplasia, Figure 3. Somatotropes presentation of GH1, Error.txt: Error:lineno:2 please check mismatch Error:lineno:4 please check mismatch

Replies are listed 'Best First'.
Re: Perl: How to compare two files
by Athanasius (Archbishop) on Aug 26, 2014 at 07:26 UTC

    This is easily done:

    #! perl use strict; use warnings; my $file1 = 'file1.txt'; my $file2 = 'file2.txt'; my $error = 'error.txt'; open(my $in1, '<', $file1) or die "Cannot open file '$file1' for readi +ng: $!"; open(my $in2, '<', $file2) or die "Cannot open file '$file2' for readi +ng: $!"; open(my $out, '>', $error) or die "Cannot open file '$error' for writi +ng: $!"; my $lineno = 1; while (my $line1 = <$in1>) { my $line2 = <$in2>; printf $out "Error:lineno:%d please check mismatch\n", $lineno unless $line1 eq $line2; ++$lineno; } close $out or die "Cannot close file '$error': $!"; close $in2 or die "Cannot close file '$file2': $!"; close $in1 or die "Cannot close file '$file1': $!";

    However, why reinvent the wheel? If the format of the output is acceptable to you, prefer a module such as Text::Diff:

    use strict; use warnings; use Text::Diff; my $error = 'error.txt'; my $diff = diff "file1.txt", "file2.txt"; open(my $out, '>', $error) or die "Cannot open file '$error' for writi +ng: $!"; print $out $diff; close $out or die "Cannot close file '$error': $!";

    Output:

    --- file1.txt Tue Aug 26 17:12:02 2014 +++ file2.txt Tue Aug 26 17:12:22 2014 @@ -1,6 +1,6 @@ Figure 1. -Somatotropes are organized into. +children with acquired organized into. Figure 2. -Comparing two xml files organized into. +Severe anterior hypoplasia, Figure 3. Somatotropes presentation of GH1,

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      I tried with text::diff, file::compare, list::compare and hash of array concept and module but the output I want in error.txt is just the position of mismatch like line no., The logic might be check mismatch line by line and whatever the mismatch found just grab that line no. of file 2 and print in the error file one by one.

      I really appreciate that you help me

      but can you do some modification

      thanks a lot

        See also Algorithm::Diff, it makes it possible to define what to do on a match or mismatch.

        but can you do some modification
        This is not a code writing service. What about you making some modifications?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl: How to compare two files
by thanos1983 (Parson) on Aug 26, 2014 at 09:19 UTC

    Hello Mjpaddy,

    Really nicely pointed out by Anonymous Monk (DIFF), under the assumption that you are running a Linux OS. I you are not running Linux an alternative would be to install Cygwin and execute the commands through there, or simply with the command FC file_1 file_2. More information can be found here fc.

    Also a Perl solution by Athanasius the (Text::Diff).

    But I could resist posting another alternative solution, that I found extremely useful not only comparing files but also for general purposes.

    The Array::Utils module where you compare arrays. It provides you with the ability to compare elements of each array.

    Sample of file_1.txt based on your description.

    This is line 1. Another line number 2. This is also another line 3.

    Sample of file_2.txt based on your description.

    This is line 1. Another line number 5. This is also another line 3.

    Finally the code with the output based only two options. Read the module it can provide you with alternative output, I choose these two for demonstration purposes.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Array::Utils qw(:all); my $file_1 = "sample-1.txt"; my $file_2 = "sample-2.txt"; my $file_3 = "error.txt"; open (my $in_1, "<" , $file_1) or die "Could not open: ".$file_1." - $!\n"; open (my $in_2, "<" , $file_2) or die "Could not open: ".$file_2." - $!\n"; open (my $out, ">>" , $file_3) or die "Could not open: ".$file_3." - $!\n"; my @data_1 = <$in_1>; chomp(@data_1); my @data_2 = <$in_2>; chomp(@data_2); # symmetric difference my @diff = array_diff(@data_1, @data_2); print Dumper(\@diff); # get items from array @data_1 that are not in array @data_2 my @minus = array_minus( @data_1 , @data_2 ); print Dumper(\@minus); foreach $_ (@diff) { print $out $_ . "\n"; } foreach $_ (@minus) { print $out $_ . "\n"; } #print $out @diff; #print $out @minus; close ($in_1) or die ("Could not close: ".$file_1." - $!\n"); close ($in_2) or die ("Could not close: ".$file_2." - $!\n"); close ($out) or die ("Could not close: ".$file_3." - $!\n"); __END__ $VAR1 = [ 'Another line number 2.', 'Another line number 5.' ]; $VAR1 = [ 'Another line number 2.' ];

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Perl: How to compare two files (diff)
by Anonymous Monk on Aug 26, 2014 at 07:24 UTC