16

Say I have two files. file1.txt and file2.txt

Both files contains a list of shoe brands name (1000+ names), like this:

brand1 brand2 brand3 brand...

Now - I want to compare file1 to file2, delete all the reoccurring entries and only show me Whats in files1 that's not in file2 and vice versa.

The goal in other words is to see what's not in the opposite file since these entries is going to be typed manually into a product backoffice for two different categories so that they'll match/be the same in the end.

3
  • 3
    IMO this would be far easier to accomplish in Excel if you can copy all your data into it or save the TXTs as CSVs. It can easily sort, remove duplicates and I'm sure column comparison would not be hard to accomplish either.
    – Karan
    Mar 7, 2013 at 20:08
  • The following link may be useful: superuser.com/a/290445
    – akjain
    Dec 12, 2013 at 10:00
  • would a different tool be suitable? You could do this easily in a few lines of python, for example (read each brand from each file and save into a set, then print the set)
    – Baldrickk
    Feb 25, 2020 at 15:58

5 Answers 5

10

Would the plugin "Compare" of Notepad++ would do the trick?

You can install it from the menu of Notepad++ plugins=> Plugin Manager=> Compare 1.5.6

Here's the official description: A very useful diff plugin to show the difference between 2 files (side by side). Author: Ty Landercasper, now maintained and updated by Jean-Sebastien Leroy Source: http://sourceforge.net/projects/npp-plugins/files/ComparePlugin/Compare_1_5_5_src.zip/download

1
  • 2
    Unfortunately, I don't think it does. The Compare plugin merely highlights the differences between two files, but offers no tools to make selections or edits based on its results. While certainly helpful, I'm afraid the task is still very tedious for over one thousand brand names. Mar 7, 2013 at 12:04
4

An old question, but...

  1. Compare the files in WinMerge
  2. Tools -> Generate Patch (save this)
  3. The patch has changes from both, but also extra markup. In notepad++, do the following replaces:

        Search Mode:  Regular Expression
        Find What:    ^[0-9-].*$
        Replace With: <blank>
        Replace All
    

    .

        Search Mode:  Regular Expression
        Find What:    (<|>)
        Replace With: <blank>
        Replace All
    
  4. Use the TextFX plugin in notepad++ either do a Tools->case-insensitive sort (output UNIQUE option selected), or Edit->Delete blank lines

Bit mungy, but I've yet to find a tool that will do this in one click.

2

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.

0

If Unix is available to you, you could try these simple combinations of simple commands; tr, sort, and comm.

First, convert the file from horizontally separated to vertically separated:

tr '[:blank:]' '\n' < file1.txt > /tmp/file1.vertical
tr '[:blank:]' '\n' < file2.txt > /tmp/file2.vertical

Then sort the files:

sort /tmp/file1.vertical > /tmp/file1.sorted
sort /tmp/file2.vertical > /tmp/file2.sorted

Now you can see what's in file1 that's not in file2

comm -23 /tmp/file1.sorted /tmp/file2.sorted

Or see what's in file2 that's not in file1

comm -13 /tmp/file1.sorted /tmp/file2.sorted

If you want the output in the same horizontal format you started with, you can do this:

comm -23 /tmp/file1.sorted /tmp/file2.sorted | tr '\n' ' '
comm -13 /tmp/file1.sorted /tmp/file2.sorted | tr '\n' ' '

When you are done, you could delete the temporary files you created:

rm /tmp/file1.vertical /tmp/file2.vertical /tmp/file1.sorted /tmp/file2.sorted
0

For others looking for something similar and not tied to Notepad++, this excellent answer by iphonedroid shows diff should be able to do the job:

To show additions and deletions without context, line numbers, +, -, <, > ! etc, you can use diff like this:

diff --changed-group-format='%<%>' --unchanged-group-format='' a.txt b.txt 

For example, given two files:

a.txt

Common
Common
A-ONLY
Common

b.txt

Common
B-ONLY
Common
Common

The following command will show lines either removed from a or added to b:

diff --changed-group-format='%<%>' --unchanged-group-format='' a.txt b.txt 

# Output
B-ONLY
A-ONLY

This slightly different command will show lines removed from a.txt:

diff --changed-group-format='%<' --unchanged-group-format='' a.txt b.txt 

# Output
A-ONLY

Finally, this command will show lines added to a.txt

diff --changed-group-format='%>' --unchanged-group-format='' a.txt b.txt 

# Output
B-ONLY

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .