Created
October 31, 2016 23:02
-
-
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"`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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