Skip to content

Instantly share code, notes, and snippets.

@adamryman
Created October 31, 2016 23:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamryman/1de22e36a14c29da2f41c8512cb86b6d to your computer and use it in GitHub Desktop.
Save adamryman/1de22e36a14c29da2f41c8512cb86b6d to your computer and use it in GitHub Desktop.
`ag -Qs "THIS"`, I want to replace all occurrences of "THIS" with "THAT". `Just do ag-replace "THIS" THAT"`
#!/bin/bash
usage() {
echo "Usage: $(basename $0) \"THIS\" \"THAT\"";
echo "Outputs files modified"
exit 1;
}
replace() {
excaped1=$(echo "$1" | sed -e 's/[\/&]/\\&/g');
excaped2=$(echo "$2" | sed -e 's/[\/&]/\\&/g');
# list files with this literal string, case sensitive
files=$(ag "$1" -Qls)
echo "$files" | while read line; do
echo "$line"
sed -i "s/$excaped1/$excaped2/g" "$(pwd)"/"$line";
done;
}
if [ "$#" -ne 2 ]; then
usage;
exit 1;
else
replace "$@";
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment