Recursively find and replace text in files under a specific folder with preview of changed data in dry-run mode
Example Usage
See what is going to change (dry run):
find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --dryrun
Do actual replacement:
find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12"
Do actual replacement and create backup files:
find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --create-backup
Same action as previous command with short-hand syntax:
find_replace.py -d project/myfolder -s "\d{4}-\d{2}-\d{2}" -r "2012-12-12" -b
Output of find_replace.py -h
:
DESCRIPTION:
Find and replace recursively from the given folder using regular expressions
optional arguments:
-h, --help show this help message and exit
--dir DIR, -d DIR folder to search in; by default current folder
--search-regex SEARCH_REGEX, -s SEARCH_REGEX
search regex
--replace-regex REPLACE_REGEX, -r REPLACE_REGEX
replacement regex
--glob GLOB, -g GLOB glob pattern, i.e. *.html
--dryrun, -dr don't replace anything just show what is going to be
done
--create-backup, -b Create backup files
USAGE:
find_replace.py -d [my_folder] -s <search_regex> -r <replace_regex> -g [glob_pattern]
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #!/usr/bin/env python
import os
import fnmatch
import sys
import shutil
import re
import argparse
def find_replace(cfg):
search_pattern = re.compile(cfg.search_regex)
for path, dirs, files in os.walk(os.path.abspath(cfg.dir)):
for filename in fnmatch.filter(files, cfg.glob):
pardir = os.path.normpath(os.path.join(path, '..'))
pardir = os.path.split(pardir)[-1]
print '[%s]' % pardir,
filepath = os.path.join(path, filename)
#backup orig file
if cfg.create_backup:
backup_path = filepath + '.bak'
while os.path.exists(backup_path):
backup_path += '.bak'
print 'DBG: creating backup', backup_path
shutil.copyfile(filepath, backup_path)
with open(filepath) as f:
data = f.read()
all_matches = search_pattern.findall(data)
if all_matches:
one_match = all_matches[0]
print 'Found {} matches in file {}'.format(len(all_matches), filename)
if not cfg.dryrun:
with open(filepath, "w") as f:
print 'DBG: replacing in file', filepath
# s = s.replace(search_pattern, replacement)
new_data = search_pattern.sub(cfg.replace_regex, data)
f.write(new_data)
else:
for line in data.split('\n'):
if one_match in line:
print " OLD: {}".format(line.strip())
print " NEW: {}".format(search_pattern.sub(cfg.replace_regex, line).strip())
else:
print 'File {} does not contain search regex "{}"'.format(filename, cfg.search_regex)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='''DESCRIPTION:
Find and replace recursively from the given folder using regular expressions''',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''USAGE:
{0} -d [my_folder] -s <search_regex> -r <replace_regex> -g [glob_pattern]'''.format(os.path.basename(sys.argv[0])))
parser.add_argument('--dir', '-d',
help='folder to search in; by default current folder',
default='.')
parser.add_argument('--search-regex', '-s',
help='search regex',
required=True)
parser.add_argument('--replace-regex', '-r',
help='replacement regex',
required=True)
parser.add_argument('--glob', '-g',
help='glob pattern, i.e. *.html',
default="*.*")
parser.add_argument('--dryrun', '-dr',
action='store_true',
help="don't replace anything just show what is going to be done",
default=False)
parser.add_argument('--create-backup', '-b',
action='store_true',
help='Create backup files',
default=False)
config = parser.parse_args(sys.argv[1:])
find_replace(config)
|