in reply to Perl: How to compare two files

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,

Replies are listed 'Best First'.
Re^2: Perl: How to compare two files
by Mjpaddy (Acolyte) on Aug 26, 2014 at 09:05 UTC

    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?
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ