Skip to content

Instantly share code, notes, and snippets.

@roshangautam
Last active August 29, 2015 14: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 roshangautam/12d7d05ba400ff7120e8 to your computer and use it in GitHub Desktop.
Save roshangautam/12d7d05ba400ff7120e8 to your computer and use it in GitHub Desktop.
search-and-replace-in-files.sh
#!/bin/bash
if [[ $# -ne 1 ]]
then
echo "Usage $0 path/to/your/directory" # path parameter missing
exit 2
fi
directory=$1
searchterms=("search" "terms" "can be anything")
replaceterms=("replace" "with" "these terms")
if [[ ${#searchterms[@]} != ${#replaceterms[@]} ]]
then
echo "Number of Search Terms and Replace terms are not equal";
exit 2;
fi
i=0
while [ $i -lt ${#searchterms[*]} ]; do
echo Processing "${searchterms[$i]}":"${replaceterms[$i]}"
grep --exclude-dir=".git" --exclude=\*.sh -r -l "${searchterms[$i]}" $directory | sort | uniq | xargs perl -pi -e "s/""${searchterms[$i]}""/""${replaceterms[$i]}/g"
i=$(( $i + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment