I have a file1 which has text as below
boy men women
girl women men
I have a file2 which has text as below
good babyboy
good babygirl
bad babyboy
bad babygirl
xyz
abc
Now I want to search for the lines in file2 with first word in file1 (boy or girl) along with the word good and print out those lines in file3.
file3 should have
good babyboy
good babygirl
Can someone help me with the code.
I tried the one below
def main():
input_file_1 = 'file1'
input_file_2 = 'file2'
output_file = 'file3'
with open(input_file_1, 'r') as f:
lines_1 = f.readlines()
with open(input_file_2, 'r') as f:
lines_2 = f.readlines()
with open(output_file, 'w') as f:
for line_1 in lines_1:
words = line_1.split()
for line_2 in lines_2:
if words[0] in line_2:
if 'good' in line_2:
f.write(line_2)
I don't get any output :(