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.
Related StackOverflow answer:
http://stackoverflow.com/questions/4427542/how-to-do-sed-like-text-replace-with-python/35157760#35157760