Skip to content

Instantly share code, notes, and snippets.

@anon987654321
Last active September 25, 2020 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anon987654321/aa45872758da69025af90c9fe60f089c to your computer and use it in GitHub Desktop.
Save anon987654321/aa45872758da69025af90c9fe60f089c to your computer and use it in GitHub Desktop.
#!/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