Coderja 1 week ago
Coderja #tutorial

Bulk Remove line containing

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

  1. Set Up Python: Ensure you have Python installed on your system. You can download it from python.org.
  2. Create a Python File: Copy the above script into a new file, for example, remove_lines.py.
  3. Update File Paths: Change the input_file_path and output_file_path variables to point to your actual input and output file paths.
  4. 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.


Menambahkan baris code kedalam file

Menambahkan baris code kedalam file

defaultuser.png
Coderja
1 week ago
Reverse Domain

Reverse Domain

defaultuser.png
Coderja
3 weeks ago
video

Cara membuat Google Drive API

Coderja
Coderja
3 weeks ago
Url Extractor Python Script

Url Extractor Python Script

defaultuser.png
Coderja
3 weeks ago
How to move the post title below content wordpress

How to move the post title below content wordpress

defaultuser.png
Coderja
1 week ago