Subtract one file from another

WandersFar
2012-11-14
2012-11-15
  • WandersFar

    WandersFar - 2012-11-14

    How can I take the difference between two files?

    For example, if I have File1.txt:
    dog
    cat
    mushroom
    banana
    chicken
    pineapple

    And I have File2.txt:
    cat
    banana
    chicken

    How can I subtract File2.txt from File1.txt to yield File3.txt:
    dog
    mushroom
    pineapple

    In other words, is there a way to get the Compare plugin to spit-out the differences between files in a separate file? Thanks for any help.

     
  • Loreia2

    Loreia2 - 2012-11-15

    You can use this simple Python script:

    ~~~~~~~~~~
    file1 = """dog
    cat
    mushroom
    banana
    chicken
    pineapple"""

    file2 = """cat
    banana
    chicken"""

    list1 = file1.split()
    list2 = file2.split()

    diff = set(list1) - set(list2)

    print diff
    ~~~~~~~~

    If you want to open a file from disk, use something like:

    ~~~~~
    file1 = open("path to file1", "r")
    ~~~~

    I hope that helps.

    BR
    Loreia

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks