I'm using the below script to search and replace string in a folder. How can i modify the code to read read multiple $searchname and $replacename from a text file and replace the words in original folders?
here are the folders:
main folder: /USERS/path/to/test/
inside test folder :
data.txt original/ script.sh tmp/
inside original folder :
file1.txt file2.php ...........
inside data.txt:
a1 a2
b1 b2
c1 c2
script for search and replace:
!/bin/bash
FOLDER="/Users/path/to/test/original/"
echo "******************************************"
echo enter word to replace
read searchterm
echo enter word that replaces
read replaceterm
for file in $(grep -l -R $searchterm $FOLDER)
do
sed -e "s/$searchterm/$replaceterm/g" $file > /Users/path/to/test/tmp/tempfile.tmp
mv /Users/path/to/test/tmp/tempfile.tmp $file
echo "Modified: " $file
done
echo " *** All Done! *** "
Thanks in advance.
sed -i -e "s/$search/$replace/g" $fileto do thesedin-place, meaning no need to> tempfileand rename.