HOW-TO Extract Lines from File A that Contains Words in File B?
#2
I think you might be able to achieve this playing around with some of the hashcat utilities but here is a short python snippet you can use as well:
Code:
#!/usr/bin/env python import re fileA = 'fileA.txt' # Your main input file fileB = 'fileB.txt' # Your file full of stuff to match against fileC = 'fileC.txt' # Output file we will save matching lines to fr=open(fileA) # File Reader Handle fw=open(fileC, 'w+') # File Writer Handle tokens_to_match = open(fileB).readlines() # Read All Lines from FileB into an Array # Iterate line by line in FileA for line in fr: # Check if any matches from fileB exist for token in tokens_to_match: # If match, then log the matching line to fileC if re.search(token.strip(), line.strip()): fw.write(line.strip() + "\n") fr.close() fw.close()

Just edit the filenames and paths for fileA, fileB (optionally fileC) and then run:
python scriptname.py

When it is done you should find fileC.txt in the same directory as the script with your matched lines.

Hope that helps a bit....


Messages In This Thread
RE: HOW-TO Extract Lines from File A that Contains Words in File B? - by iRuser - 01-04-2015, 12:11 AM