diff options
author | Ben Sima <ben@bsima.me> | 2021-05-17 08:06:00 -0400 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2021-05-22 06:03:11 -0400 |
commit | 52087d5bcad3ab7ca34ca0292c89b3f8c622cce1 (patch) | |
tree | e12b478b85585c4933cf6ad0c25b31a37fc27c83 /replace | |
parent | e2dda5e626eb336d910ebbbd1547f4a18c4d7926 (diff) |
add raw regex and fix bug
Diffstat (limited to 'replace')
-rwxr-xr-x | replace | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -1,7 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ replace stuff in a file or files """ +# maybe i should just use https://replace.richardlloyd.org.uk/ import argparse import difflib import fileinput @@ -30,12 +31,21 @@ args = cli.parse_args() differ = difflib.Differ() + +def raw(s): + return r"{}".format(s) + + +def log(s): + sys.stderr.write(s) + + for line in fileinput.input(files=args.files, inplace=not args.dry): if args.regex: - replacement = re.sub(args.old, args.new, line) + replacement = re.sub(raw(args.old), args.new, line, flags=re.MULTILINE) else: replacement = line.replace(args.old, args.new) - if (args.verbose or args.dry) and args.old in line: + if (args.verbose or args.dry) and (line != replacement): diff = list(differ.compare([line], [replacement])) sys.stderr.write(f"{fileinput.filename()}:{fileinput.lineno()}:\n") sys.stderr.writelines(diff) |