2017-04-21 22:59:04 -07:00
|
|
|
#!/usr/bin/env bash
|
2017-12-05 22:39:10 -08:00
|
|
|
set -e
|
|
|
|
unset CDPATH
|
|
|
|
|
2016-03-03 07:46:27 -08:00
|
|
|
# Find and replace by a given list of files.
|
|
|
|
#
|
|
|
|
# replace foo bar **/*.rb
|
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
main() {
|
|
|
|
local search="$1"
|
|
|
|
local replace="$2"
|
|
|
|
shift 2
|
|
|
|
|
|
|
|
for item in $(ag -l --nocolor "$search" "$@"); do
|
|
|
|
sed -i "s%${search//%/\\%}%${replace//%/\\%}%g" "$item"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Show matches
|
|
|
|
ag "$1"
|
2016-03-03 07:46:27 -08:00
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
# Are you sure?
|
|
|
|
read -rp "Confirm search+replace? (y/n)?" choice
|
|
|
|
case $choice in
|
|
|
|
y|Y) main "$@" ;;
|
|
|
|
*) >&2 echo "Aborted"; exit ;;
|
|
|
|
esac
|