Skip to content

Instantly share code, notes, and snippets.

@Mikhael-Danilov
Forked from evansd/gist:1639992
Last active December 16, 2015 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mikhael-Danilov/5389883 to your computer and use it in GitHub Desktop.
Save Mikhael-Danilov/5389883 to your computer and use it in GitHub Desktop.
simple script to do simple replace in source tree
#!/bin/bash
if [ -z "$1" -o -z "$2" ]; then
echo "Usage: substitute FROM_STRING TO_STRING [OPTION]..."
echo
echo "Replace all occurrences of FROM_STRING (a sed-compatible regular"
echo "expression) with TO_STRING in all files for which ack matches"
echo "FROM_STRING."
echo
echo "Any additional options are passed directly to ack (e.g.,"
echo " --type=html would only run the substitution on html files)."
exit 1
fi
# Escape forward slashes for sed
FROM_STRING=${1//\//\\/}
TO_STRING=${2//\//\\/}
echo $FROM_STRING
echo $TO_STRING
shift 2
ack -l --print0 "$@" "$FROM_STRING" | xargs -0 -n 1 sed -i -e "s/$FROM_STRING/$TO_STRING/g"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment