Need Help Editing a Large Text File

john
2011-10-18
2012-11-14
  • john
    john
    2011-10-18

    Good day.  I could really use your help on this one.   I have a stats text file in the following format.

    ID=1000000
    Name=Name1
    Field1=Value1
    ...(Fields 2 to 25)
    Field26=Value26
    ID=1000001
    Name=Name2
    Field1=Value1
    ...(Fields 2 to 25)
    Field26=Value26
    ...goes up to 15000
    

    I have an active people text file separated by line breaks. 

    Name2
    Name5
    Name11
    Name12
    ...goes up to 1400
    

    I need to be able to delete records from the stats text file (ID,Name,Fields1 to 26) if the name is not found in the active people text file.  In the example above, the associated record for Name1 should be deleted since it's not in the active people text file.

    I've been trying my best to figure this out for a hours on end.  Is there a solution that won't require me to use find, copy, paste and switch between the two files 1400 times?   Please help.  Thank you.

     
  • Yep…

    Put your names file in the right hand pane (right click the tab, Move to other view).

    Then, with the python script plugin (install it with plugin manager if you've not already) - then just add a new script, and run it.

    import re
    def checkname(m):
        global names;
        if m.group(1) not in names:
            return ""
        else:
            return m.group(0)
    
    # Read in the valid names 
    names = editor2.getText().split('\r\n')
    text = editor1.getText();
    editor1.setText(re.sub(r"ID=[0-9]+\r\nName=(.*)\r\n(Field[0-9]+=.*\r\n)+", checkname, text, 0))
    

    hth,
    Dave.