in reply to Re: Getting error when trying to compare two files
in thread Getting error when trying to compare two files

Can you please let me know what needs to be done for comparing the content of the files line by line
  • Comment on Re^2: Getting error when trying to compare two files

Replies are listed 'Best First'.
Re^3: Getting error when trying to compare two files
by Happy-the-monk (Canon) on Nov 13, 2015 at 09:40 UTC

    What is the goal you are trying to achieve?

    If you want to know the differences between the files, Algorithm::Diff is better suited than File::Compare.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

      Hi, I want to compare all the content and report all differences. Also I am facing some challenge in comparing like when I have captured the content from Linux, it may not match against output from Windows (and vice-versa) since the line ending pattern of Windows would contain a \r\n (That’s a carriage return followed by linefeed) instead of a plain \n in the case of Linux. Please let me know how I can handle these Also can you please let me know how to use Algorithm::Diff

        I want to compare all the content and report all differences.

        If you feel better at home with external commands, diff is the one you should be looking at.

        Also I am facing some challenge in comparing like when I have captured the content from Linux, it may not match against output from Windows (and vice-versa) since the line ending pattern of Windows would contain a \r\n (That’s a carriage return followed by linefeed) instead of a plain \n in the case of Linux. Please let me know how I can handle these

        There are a few options for that. Again if you prefer external commands, look for dos2unix or unix2dos. In Perl, you can substitute with s/\015\012/\012/ - depending on your beforehand knowledge of the size of the files, if they are small, you can treat the whole file in one go or use a while loop to save memory and sacrifice time instead.

        can you please let me know how to use Algorithm::Diff

        Have you tried using some example from the Algorithm::Diff documentation? Did it produce an unforeseen result?

        Again, the question of size may hit you here again. Just for the first go, I'll hope it doesn't ;-)

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

      I have my expected output in one file which is of some 10 lines and when I execute perl program it will generate output which I am copying to a different file.

      Now I need to compare actual vs expected? How I can achieve this?

        As with Laurent_R's question you haven't answered my question either.

        Tell us what your "compare actual vs expected" should be done, what you expect that to answer back, and we can probably show you the way.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

Re^3: Getting error when trying to compare two files
by Laurent_R (Canon) on Nov 13, 2015 at 09:33 UTC
    Sure, we can help on that, but first tell us what you really want: do you want to just report that the files are equal or not equal, or do you want to just report where (which line) the first difference is found, or do you want to list all the differences? The first two cases are fairly easy, the last one is more complicated and implies some pre-requirements.

      Hi, I want to compare all the content and report all differences. Also I am facing some challenge in comparing like when I have captured the content from Linux, it may not match against output from Windows (and vice-versa) since the line ending pattern of Windows would contain a \r\n (That’s a carriage return followed by linefeed) instead of a plain \n in the case of Linux. Please let me know how I can handle these

        First, one question: are your files guaranteed to be in the same order? If not, can you sort them (using for example the Unix sort utility) according to the same key(s) before you start? If yes, then you can build on the algorithm I have shown above, reading both files in parallel. Please let us know if you have any difficulty with this.

        When dealing with this type of problem, I usually follow a two-step approach: first read the two files in parallel in order to extract the "orphans", i.e. lines which are in one file and not in the other in accordance with a comparison key. This produces two files without orphans (therefore having the same line keys and the same number of lines), which I can then easily compare for differences between the lines (for fields not part of the comparison key). This two-step approach is not necessary, it can be done in one go, but it usually makes it easier to process the data and deal with edge cases.

        As for line endings, the easiest is probably to start by eliminating all line endings when reading the lines, to make the comparisons independent of line endings, and to add the line endings at the end, when outputting the result.

        A simple instruction such as:

        $line =~ s/[\r\s]+//g;
        should remove any line ending, whether Windows or Unix (or Mac), from the $line string.

      I have my expected output in one file which is of some 10 lines and when I execute perl program it will generate output which I am copying to a different file.

      Now I need to compare actual vs expected? How I can achieve this?

        You haven't answered my questions.

        The simple cases (just find if there is a difference or where the first difference is)can be dealt with something like this, which reads both files in parallel (assuming you have opened both files already):

        while (my $line1 = <$FILE1>) { my $line2 = <$FILE2>; print "$line1 missing in file2\n" and last unless defined $line2; next if $line1 eq $line2; print "Difference found on line $.\n"; # you may want to exit here, depending on what you need }
        It might be more complicated if you need to output more information.

        You may also want to add some code at the end to figure out if $FILE2 has more lines than $FILE1 at the end. But you haven't said enough on your requirement for me to be willing to look at all the edge cases.

        Please also note that there are some modules to do that, as you already know (judging from your original post).