Created
February 26, 2023 21:08
-
-
Save apaz-cli/ba4aa14f66d457b88730448c7f52aff9 to your computer and use it in GitHub Desktop.
Search and Replace script
This file contains hidden or 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
| # USAGE: | |
| # py sr.py <target> <output> (<search> <replace>)* | |
| import sys | |
| def fopen(fname): | |
| with open(fname, 'r') as file: | |
| return file.read() | |
| def fsave(newfname, data): | |
| with open(newfname, 'w') as file: | |
| file.write(data) | |
| def searchReplace(terms, data): | |
| for search, replace in terms: | |
| data = data.replace(search, replace) | |
| return data | |
| data = fopen(sys.argv[1]) | |
| terms = [] | |
| current = 2 | |
| while current: | |
| current += 1 | |
| if current == len(sys.argv): | |
| break | |
| s = sys.argv[current] | |
| current += 1 | |
| if current == len(sys.argv): | |
| break | |
| r = sys.argv[current] | |
| terms.append((s, r)) | |
| data = searchReplace(terms, data) | |
| fsave(sys.argv[2], data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment