我使用下面的脚本来搜索和替换文件夹中的字符串。我如何修改代码以读取从文本文件中读取多个$searchname和$replacename并替换原始文件夹中的单词?
以下是文件夹:
main folder: /USERS/path/to/test/
测试文件夹内:
data.txt original/ script.sh tmp/
原始文件夹内:
file1.txt file2.php ...........
data.txt内部:
a1 a2
b1 b2
c1 c2
搜索和替换脚本:
!/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! *** "
提前谢谢。
将搜索和替换术语的文件(我将称之为search_and_replace.txt)设置为:
searchone/replaceonesearchtwo/replacetwofoo/bar。。。
然后你的bash脚本变成:
/垃圾桶/垃圾桶FOLDER="/Users/path/to/test/original/"同时读取行;做对于"find$FOLDER-type f"中的文件;做sed-i-e"s/$line/g"$文件已完成done<search_and_replace_terms.txt
我认为没有必要用grep进行预搜索。