28 lines
461 B
Bash
Executable file
28 lines
461 B
Bash
Executable file
#!/usr/bin/env bash
|
|
set -e
|
|
unset CDPATH
|
|
|
|
# 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"
|
|
|
|
# Are you sure?
|
|
read -rp "Confirm search+replace? (y/n)?" choice
|
|
case $choice in
|
|
y|Y) main "$@" ;;
|
|
*) >&2 echo "Aborted"; exit ;;
|
|
esac
|