-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreplace-text-block
More file actions
executable file
·40 lines (29 loc) · 1.26 KB
/
replace-text-block
File metadata and controls
executable file
·40 lines (29 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/python
# This script replaces text in a file. The search text and replacement text
# are in two other, separate files.
import os
import sys
import argparse
parser=argparse.ArgumentParser(
description='''Searches through a text file for a block of text and replaces it with another text block.''')
parser.add_argument('change_file', help='File to be changed')
parser.add_argument('search_text_file', help='File that contains the text to search for')
parser.add_argument('replacement_text_file', help='File that contains the new (replacement) text')
args=parser.parse_args()
# Read in the search text
with open(args.search_text_file, 'r') as search_text_file :
search = search_text_file.read()
# Read in the replacement text
with open(args.replacement_text_file, 'r') as replacement_text_file :
replacement = replacement_text_file.read()
# Read in the file to be changed
with open(args.change_file, 'r') as change_file :
filedata = change_file.read()
# Replace the target string (only first occurrence)
newfiledata = filedata.replace(search, replacement, 1)
if newfiledata == filedata :
print("File not changed: " + str(args.change_file));
sys.exit(1);
# Write the file out again
with open(args.change_file, 'w') as change_file:
change_file.write(newfiledata)