Text Line Replacer

Mereplace beberapa string dalam sebuah file txt

2024-11-04 13:56:28 - Coderja

Menggunakan Python
  1. Buat File Skrip: Buat file Python baru, misalnya replacer.py.
  2. Tulis Kode Berikut:


import sys

# Daftar string yang ingin diganti
strings_to_replace = [
    "wp-admin/user-new.php",
    "wp-admin/install.php",
    "wp-admin/profile.php",
    "wp-admin/post.php",
    "wp-admin/admin.php",
    "wp-admin/options-writing.php",
    "wp-admin/user-edit.php",
    "wp-admin/options-general.php",
    "wp-admin/update-core.php",
    "wp-admin/plugin-install.php",
    "wp-admin/post-new.php",
    "wp-admin/themes.php",
    "wp-admin/plugins.php",
    "wp-admin/update.php",
    "wp-admin/theme-install.php",
    "wp-admin/setup-config.php",
    "wp-admin/admin-post.php?action=",
    "wp-admin/options.php",
    "wp-admin/admin-ajax.php",
    "wp-admin/edit.php",
    "wp-admin/index.php",
    "wp-admin/networkupdate.php"
]

# String pengganti
replacement_string = "wp-login.php"

# Cek argumen
if len(sys.argv) != 3:
    print("Usage: python replacer.py <input_file> <output_file>")
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

# Baca file dan ganti string
with open(input_file, 'r', encoding='utf-8') as file:
    file_contents = file.read()

for string in strings_to_replace:
    file_contents = file_contents.replace(string, replacement_string)

# Tulis hasil ke file baru
with open(output_file, 'w', encoding='utf-8') as file:
    file.write(file_contents)

print(f"Penggantian selesai. Hasil disimpan di {output_file}.")


Jalankan Skrip: Buka terminal atau command prompt, navigasikan ke direktori di mana file replacer.py berada, dan jalankan perintah berikut:


python replacer.py hugefiles.txt result.txt


Menggunakan PowerShell

Jika Anda ingin menggunakan PowerShell, berikut adalah cara untuk melakukannya:

  1. Buka PowerShell dan buat skrip baru, misalnya replacer.ps1.
  2. Tulis Kode Berikut:
param (
    [string]$inputFile,
    [string]$outputFile
)

# Daftar string yang ingin diganti
$stringsToReplace = @(
    "wp-admin/user-new.php", 
    "wp-admin/install.php", 
    "wp-admin/profile.php", 
    "wp-admin/post.php", 
    "wp-admin/admin.php", 
    "wp-admin/options-writing.php", 
    "wp-admin/user-edit.php", 
    "wp-admin/options-general.php", 
    "wp-admin/update-core.php", 
    "wp-admin/plugin-install.php", 
    "wp-admin/post-new.php", 
    "wp-admin/themes.php", 
    "wp-admin/plugins.php", 
    "wp-admin/update.php", 
    "wp-admin/theme-install.php", 
    "wp-admin/setup-config.php", 
    "wp-admin/admin-post.php?action=", 
    "wp-admin/options.php", 
    "wp-admin/admin-ajax.php", 
    "wp-admin/edit.php", 
    "wp-admin/index.php", 
    "wp-admin/networkupdate.php"
)

# String pengganti
$replacementString = "wp-login.php"

# Baca file
$fileContent = Get-Content -Path $inputFile -Raw

# Ganti string
foreach ($string in $stringsToReplace) {
    $fileContent = $fileContent -replace [regex]::Escape($string), $replacementString
}

# Tulis hasil ke file baru
Set-Content -Path $outputFile -Value $fileContent

Write-Host "Penggantian selesai. Hasil disimpan di $outputFile."


Jalankan Skrip: Di PowerShell, jalankan perintah berikut:


.\replacer.ps1 -inputFile "hugefiles.txt" -outputFile "result.txt"
Catatan


More Posts