Skip to content

Commit 961fb35

Browse files
committed
CR add script
1 parent f3d6d7a commit 961fb35

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

scripts/file-search-replace.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/python
2+
3+
# ***********************************************************************
4+
# Copyright (c) 2017 Unity Technologies. All rights reserved.
5+
#
6+
# Licensed under the ##LICENSENAME##.
7+
# See LICENSE.md file in the project root for full license information.
8+
# ***********************************************************************
9+
10+
11+
# Read the specified file and replace all instances of searchstr with replacestr.
12+
13+
import os
14+
import sys
15+
import re
16+
17+
if len(sys.argv) < 4:
18+
sys.stderr.write("USAGE: {} input/path/to/file searchstr replacestr\n".format(sys.argv[0]))
19+
sys.stderr.write("Performs a search replace on the input file./.\n")
20+
sys.exit(1)
21+
22+
filename = sys.argv[1]
23+
if not os.path.exists(filename):
24+
sys.stderr.write("ERROR: file {} does not exist\n".format(filename))
25+
sys.exit(1)
26+
27+
searchstr = sys.argv[2]
28+
replacestr = sys.argv[3]
29+
if not searchstr or not replacestr:
30+
sys.stderr.write("ERROR: invalid search or replace string detected")
31+
sys.exit(1)
32+
33+
with open(filename, 'r+') as filein:
34+
data=filein.read()
35+
data=re.sub(searchstr, replacestr, data, 1, re.IGNORECASE)
36+
filein.seek(0)
37+
filein.write(data)
38+
filein.truncate()

0 commit comments

Comments
 (0)