Skip to content

Instantly share code, notes, and snippets.

@ginokent
Last active August 31, 2018 03:41
Show Gist options
  • Save ginokent/c895926a7f77ab7b254b828fbda32fa2 to your computer and use it in GitHub Desktop.
Save ginokent/c895926a7f77ab7b254b828fbda32fa2 to your computer and use it in GitHub Desktop.
個人的にsedの代わりに使う
#!/usr/bin/env python
import argparse,re,sys
# opts
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--replace", action="store_true")
parser.add_argument("-b", "--before", dest="before")
parser.add_argument("-a", "--after", dest="after")
parser.add_argument("-f", "--file", dest="file")
if len(sys.argv) <= 1:
parser.print_help()
exit(1)
args = parser.parse_args()
# replace
with open(args.file, 'r') as file_content:
content_before_replace=file_content.read()
content_after_replace=re.sub(r"%s" % args.before, args.after, content_before_replace, flags=re.DOTALL)
if args.replace:
with open(args.file, 'w') as file_content_w:
file_content_w.write(content_after_replace)
else:
sys.stdout.write(content_after_replace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment