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

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.
  • Comment on Re^3: Getting error when trying to compare two files

Replies are listed 'Best First'.
Re^4: Getting error when trying to compare two files
by adalamre (Initiate) on Nov 16, 2015 at 07:17 UTC

    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.

Re^4: Getting error when trying to compare two files
by adalamre (Initiate) on Nov 13, 2015 at 10:15 UTC

    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).