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

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

  • Comment on Re^4: Getting error when trying to compare two files

Replies are listed 'Best First'.
Re^5: Getting error when trying to compare two files
by Laurent_R (Canon) on Nov 16, 2015 at 11:10 UTC
    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.