Last active
September 25, 2020 22:17
-
-
Save anon987654321/aa45872758da69025af90c9fe60f089c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env zsh | |
# | |
# REPLACES STRINGS IN TEXT-BASED FILES | |
# | |
# replace <old string> <new string> | |
# | |
setopt extendedglob | |
# Dependency check | |
function die { | |
print >&2 "$@" | |
exit 1 | |
} | |
for command in file; do | |
(( ${+commands[$command]} )) || failed+=($command) | |
done | |
if (( $#failed )); then | |
die "Not found: $failed" | |
fi | |
echo "Replacing $1 with $2..." | |
for file in **/*; do | |
# Match text files | |
if file -b $file | grep -q "text" && grep -q -- "$1" $file; then | |
# Sed in-place editing doesn't work the same way on BSD, so use temporary file instead | |
sed "s/$1/$2/g" $file > $file.tmp | |
mv -f $file.tmp $file | |
echo "$file" | |
fi | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment