Bulk Remove line containing
remove unwanted line containing word from a file
2024-11-09 21:43:11 - Coderja
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
- Set Up Python: Ensure you have Python installed on your system. You can download it from python.org.
- Create a Python File: Copy the above script into a new file, for example, remove_lines.py.
- Update File Paths: Change the input_file_path and output_file_path variables to point to your actual input and output file paths.
- Run the Script: Open your command line or terminal, navigate to the directory where your script is located, and run:
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
- The Python solution is generally more efficient for large files compared to the PowerShell approach.
- Make sure to handle file paths correctly, especially if they contain spaces or special characters.