scatterd-dotfiles/bin/replace
Blaine Schmeisser 1dc550d985 Overwrite internal field separator when replacing.
Problem:
Currently the `for` in bash will break up lists by whitespace, so if a
filename has a space in it the `for` will break it up incorrectly.

AG will separate the files with a newline character, so this temporarily
overwrites the `IFS` to be used correctly for this use case.

[Docs](https://bash.cyberciti.biz/guide/$IFS)
2016-04-15 10:36:33 -04:00

17 lines
334 B
Bash
Executable file

#!/bin/sh
#
# Find and replace by a given list of files.
#
# replace foo bar **/*.rb
find_this="$1"
shift
replace_with="$1"
shift
items=$(ag -l --nocolor "$find_this" "$@")
temp="${TMPDIR:-/tmp}/replace_temp_file.$$"
IFS=$'\n'
for item in $items; do
sed "s/$find_this/$replace_with/g" "$item" > "$temp" && mv "$temp" "$item"
done