helm/bin/replace

28 lines
461 B
Plaintext
Raw Normal View History

2017-04-21 22:59:04 -07:00
#!/usr/bin/env bash
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
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
# Are you sure?
read -rp "Confirm search+replace? (y/n)?" choice
case $choice in
y|Y) main "$@" ;;
*) >&2 echo "Aborted"; exit ;;
esac