Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Last active February 10, 2019 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlemonvh/0743a1c63d1d27df3f17 to your computer and use it in GitHub Desktop.
Save turtlemonvh/0743a1c63d1d27df3f17 to your computer and use it in GitHub Desktop.
A python replacement for a basic usage of sed: find and replace pattern
#!/usr/bin/env python
# From: https://gist.github.com/turtlemonvh/0743a1c63d1d27df3f17
from __future__ import print_function
import argparse
import fileinput
import re
import sys
import glob
if __name__ == "__main__":
description = """Replace matches in a file with a pattern.
(e.g.)
text = "cat 976 is my favorite"
pattern = "((cat|dog) \d+)"
# Use \g<name> for named group 'name'
# Use \\1 for group # 1
template = "turtle \\1"
result = "turtle 976 is my favorite"
"""
parser = argparse.ArgumentParser(description=description, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('filename', action='store', help="Filename to search in.")
parser.add_argument('input_pattern', action='store', help="Input pattern to search in.")
parser.add_argument('template', action='store', help="Template to use to replace regex patterns.")
parser.add_argument('-i', dest='in_place', action='store_true', default=False, help="Edit the file in place.")
options = parser.parse_args()
search_pattern = re.compile(options.input_pattern)
def process_func(line):
# https://docs.python.org/2/library/re.html#re.sub
print(re.sub(search_pattern, options.template, line).rstrip("\n"))
for fn in glob.glob(options.filename):
if options.in_place:
for line in fileinput.input([fn], inplace=True):
process_func(line)
else:
with open(fn) as f:
for line in f:
process_func(line)

Not sed: Find and replace with python

Find and replace using python's re.sub command.

Example usage

# Help message
python find_replace.py -h

# Check output of modifying file
python find_replace.py ~/Projects/deployments/provision-analytics-cluster.yml 'groups\[(\w+)\]' 'groups.get(\1, [])' | less

# Run in place to edit
python find_replace.py ~/Projects/deployments/provision-analytics-cluster.yml 'groups\[(\w+)\]' 'groups.get(\1, [])' -i

Note that the pattern cannot span multiple lines, but there can be multiple instances of the pattern on each line.

@turtlemonvh
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment