Last active
August 29, 2015 14:17
-
-
Save roshangautam/12d7d05ba400ff7120e8 to your computer and use it in GitHub Desktop.
search-and-replace-in-files.sh
This file contains hidden or 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
#!/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