Compare files and return only the differences using Notepad++

To substract two files in notepad++ (file1 – file2) you may follow this procedure:

  1. Recommended: If possible, remove duplicates on both files, specially if the files are big. To do this: Edit => Line operations => Sort Lines Lexicographically Ascending (do it on both files)
  2. Add ---------------------------- as a footer on file1 (add at least 10 dashes). This is the marker line that separates file1 content from file2.
  3. Then copy the contents of file2 to the end of file1 (after the marker)
  4. Control + H
  5. Search: (?m-s)^(?:-{10,}+\R[\s\S]*+|(.*+)\R(?=(?:(?!^-{10,}$)-++|[^-]*+)*+^-{10,}+\R(?:^.*+\R)*?\1(?:\R|\z))) note: use case sensitivity according to your needs
  6. Replace by: (leave empty)
  7. Select Regular expression radio button
  8. Replace All

You can modify the marker if It is possible that file1/file2 can have lines equal to the marker. In that case you will have to adapt the regular expression.

By the way, you could even record a macro to do all steps (add the marker, switch to file2, copy content to file1, apply the regex with a single button press.

Edited:

Changed the regex to add some improvements:

  • Speed related:
    • Avoid as much backtracking as possible
    • Avoid searching after the mark
  • Usability:
    • Dashes are allowed for the lines. But the separator is still ^-{10,}$
    • Works with other characters besides words

Speed comparison:

New method vs Old method

So basically 78ms vs 1.6seconds. So a nice improvement! That makes comparing Kilobyte-sized files possible.

Still you may want to use some dedicated program for comparing or substracting bigger files.

Leave a Comment

tech