Created
September 14, 2023 02:35
-
-
Save johanguse/6aface218f0bda55ee8561f647dc723e to your computer and use it in GitHub Desktop.
Replace a domain in a big SQL file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
import time | |
def replace_domain_in_large_sql(filename, old_domain, new_domain): | |
start_time = time.time() | |
temp_filename = filename + '.tmp' | |
count = 0 | |
with open(filename, 'r') as file, open(temp_filename, 'w') as temp_file: | |
for line in file: | |
new_line, replacements = re.subn(old_domain, new_domain, line) | |
temp_file.write(new_line) | |
count += replacements | |
os.remove(filename) | |
os.rename(temp_filename, filename) | |
end_time = time.time() | |
elapsed_time = round(end_time - start_time, 1) | |
print(f"Replaced {count} occurrence(s) of '{old_domain}' with '{new_domain}' in the SQL file.") | |
print(f"The operation took {elapsed_time} seconds.") | |
# usage | |
replace_domain_in_large_sql('/path-to-file.sql', 'old-domain.com', 'newdomain.com') | |
# Path: replace.py | |
# Author: Johan Guse | |
# Date: 2023-02-21 | |
# Purpose: Replace a domain in a SQL file | |
# Usage: run on terminal the file replace.py | |
# Notes: This script is not intended to be used in production | |
# environments. Use at your own risk. | |
# This script is provided as-is without warranty of any kind. | |
# This script is for educational purposes only. | |
# This script is not affiliated with any company or organization. | |
# This script is not intended to be used for malicious purposes. | |
# This script is not intended to be used for illegal purposes. | |
# This script is not intended to be used for any purpose other than |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment