Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare files and return only the differences using Notepad++

Tags:

Notepad++ has a Compare Plugin tool for comparing text files, which operates like this:

Launch Notepad++ and open the two files you wish to run a comparison check on.

Click the “Plugins” menu,

Select “Compare” and click “Compare.”

The plugin will run a comparison check and display the two files side by side, with any differences in the text highlighted.

This is a nice feature, and which I have used happily for some time. Now, I have been looking for an option to go further and select the highlighted differing lines (e.g. by deleting the non-highlighted ones), or vice versa: i.e. expunge the highlighted lines.

Is there a straightforward way to achieve this?

like image 793
Ifedi Okonkwo Avatar asked Jun 28 '15 11:06

Ifedi Okonkwo


People also ask

How do I compare files in notepad?

Open any two files (A, B) in Notepad++, which you want to compare. File B (new) gets compared to File A (old). Then, navigate to Plugins > Compare Menu > Compare. It shows the difference/comparison side by side, as shown in the screenshot.

How do I save notepad compare results?

If you want to save the results of the comparison, you can click on the File menu and select Save As. This will allow you to save the comparison as a text file, which can be useful for later reference. Comparing files in Notepad++ is a quick and easy way to see the differences between two versions of a file.

How do I enable compare options in Notepad++?

Open Notepad++, open the PluginsAdmin from Plugins menu, select Compare plugin and click Install. Notepad++ will restart and you should have the plugin in the menu.


1 Answers

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.

like image 53
Julio Avatar answered Sep 19 '22 15:09

Julio