remove unwanted line containing word from a file
Python Script
# List of words to remove lines containing them words_to_remove = [ "1h39e2crhvt994t7mr3s87jrq8kv5xqn", "1iivfsvis2e4jod04rtm9q7pl6mdnbxk", "1yeqs8kr2ltu", # Add the rest of the words here... "z8m3yp47n8ub" ] # Read the input file and write to the output file input_file_path = 'path/to/your/input_file.txt' # Change this to your input file path output_file_path = 'path/to/your/output_file.txt' # Change this to your desired output file path # Create a set for faster lookup words_set = set(words_to_remove) with open(input_file_path, 'r') as infile, open(output_file_path, 'w') as outfile: for line in infile: # Check if any word in the set is in the current line if not any(word in line for word in words_set): outfile.write(line) print(f"Lines not containing specified words have been written to {output_file_path}.")
Instructions
bash python remove_lines.py
CMD or PowerShell Alternative
If you prefer using PowerShell, you can use the following command, but it may not be as efficient for very large files:
powershell $words = @( "1h39e2crhvt994t7mr3s87jrq8kv5xqn", "1iivfsvis2e4jod04rtm9q7pl6mdnbxk", "1yeqs8kr2ltu", # Add the rest of the words here... "z8m3yp47n8ub" ) Get-Content 'path\to\your\input_file.txt' | Where-Object { $line = $_ -not ($words | ForEach-Object { $line -like "*$_*" }) } | Set-Content 'path\to\your\output_file.txt'
Notes