'Iteration over file - check if the string from other file is present in the line of source file

I have a source file that looks like this:

1   AX-75454925 0   563128
1   AX-75469048 0   573126
1   AX-75492867 0   584209
1   AX-75534944 0   605258
1   AX-75563461 0   619069
...

I also have a file with the list of ID's that looks like this:

AX-80968854
AX-75258857
AX-75259850
AX-75259941
AX-75260842
...

I want to delete lines in a source file in a way that only the lines that include the strings from the ID list are left.

I've tried to solve the problem using Python - but was only able to write the solution that checks if lines are the same in both files (not only the string, like AX-75454925).

Here's what I've tried:

outfile = open("output.txt", "a")

def checkLine(ID):        
    with open("source.map") as f:
        for line in f:
            if ID in line:
                outfile.write(line)

for ID in open("IDs.txt", "r"):
    checkLine(ID)      

Thanks,

Tom



Solution 1:[1]

Python is definitely the easier language to do this in. You're going to want to read the whole set of search terms from the one file in, and then check each line against the whole set:

# read IDs file
with open('IDs.txt') as ids_file:
    ids = {id.strip() for id in ids_file}
# make sure we don't have any empty IDs
ids = set(filter(None, ids))

# open the files
with open('extra_lines.txt') as in_file:
    with open('output.txt', 'w') as out_file
        for line in in_file:
            # check if any IDs are in the line
            if any(id in line for id in ids):
                out_file.write(line)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pi Marillion