Compare two files and delete differences



  • Hello,
    I have one file that contains account numbers and a second file that contains lines with the account numbers included along with other information on that line. All the entries in file one are in file two, but file two contains extra lines I do not need. I need to match file one to file two and delete the non-matching lines from file two. Again, file one only contains account numbers, file two contains the account numbers along with other information such as name and address.



  • @Michael-Memphis said in Compare two files and delete differences:

    I need to match file one to file two and delete the non-matching lines

    I don’t use plugins, of which there are quite a few, and maybe one of those might have what you are looking for.

    However I would do it as follows.

    1. For the file with ONLY account numbers, append an a to the account number.
    2. For the file with other additional information append a b to the account number and if required copy that account number (with b) to the front of the line.
    3. Combine both files into 1 new file.
    4. Sort the file numerically. Thus an account with “a” would appear before the corresponding line from file #2 (which has a b appended to account number).
    5. Use a regular expression (regex) to remove all lines with the a appended or a b line where there is no corresponding a line. This can be completed in either 1 or 2 regexes. Also remove the appended b to clean up those lines.

    The result will be a new file #2. However the caveat is that now the file will have accounts in possibly a different order to what they were initially before combining and sorting. This can be overcome by additional steps in the process.

    You will note that I haven’t provided any regexes, as you haven’t provided any examples of how file 1 and 2 look. I’m not suggesting providing real data (possible confidentiality issues) but as the regexes can only be matched to real data we do need examples of how the data looks in order to better support you (replace some real data with dummy info of the same type). Please read the FAQ, primarily the post titled:
    FAQ Desk: Request for Help without sufficient information to help you
    Whilst this post refers more to problems running Notepad++, the use of codes to insert examples in your post will be of help.

    Terry


Log in to reply