http://forums.devshed.com/unix-help-35/unix-find-and-replace-text-within-all-files-within-a-146179.html
# *****************************************************************************************
# find_and_replace_in_files.sh
# This script does a recursive, case sensitive directory search and replace of files
# To make a case insensitive search replace, use the -i switch in the grep call
# uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself!
# *****************************************************************************************
!/bin/bash
# **************** Change Variables Here ************
startdirectory="/home/gare/tmp/tmp2"
searchterm="search"
replaceterm="replaceTerm"
# **********************************************************
echo "******************************************"
echo "* Search and Replace in Files Version .1 *"
echo "******************************************"
for file in $(grep -l -R $searchterm $startdirectory)
do
sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
echo "Modified: " $file
done
echo " *** Yay! All Done! *** "
아래와같이 한꺼번에 바꿀수도 있다네.
find ./ -name "*.txt" -exec perl -pi -e 's/2aaaabbbcccdd/2aaaabbbcccdd33333/g' {} \; 한꺼번에 바꾸기
찾을 문자열에 홑따옴표(작은따옴표)가 들어 있으면 다음과같이 해야한다.
'test'를 $4로 바꿀때 찾는 문자열내의 정규식을 작은따옴표로 묶어주어야 한다. (AIX 6.11)
find . -name "*.txt" -exec perl -pi -e 's/'\'test\''/₩$4/g' {} \;
그런데 경우에 따라서 변경하려는 문자열도 다음과 같이 작은따옴표로 묶어야 되는 경우도 있다.
find . -name "*.txt" -exec perl -pi -e 's/'\'test\''/'₩$4'/g' {} \;
# find_and_replace_in_files.sh
# This script does a recursive, case sensitive directory search and replace of files
# To make a case insensitive search replace, use the -i switch in the grep call
# uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself!
# *****************************************************************************************
# **************** Change Variables Here ************
startdirectory="/home/gare/tmp/tmp2"
searchterm="search"
replaceterm="replaceTerm"
# **********************************************************
echo "* Search and Replace in Files Version .1 *"
echo "******************************************"
do
sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
echo "Modified: " $file
done
find ./ -name "*.txt" -exec perl -pi -e 's/2aaaabbbcccdd/2aaaabbbcccdd33333/g' {} \; 한꺼번에 바꾸기
'test'를 $4로 바꿀때 찾는 문자열내의 정규식을 작은따옴표로 묶어주어야 한다. (AIX 6.11)
find . -name "*.txt" -exec perl -pi -e 's/'\'test\''/₩$4/g' {} \;
find . -name "*.txt" -exec perl -pi -e 's/'\'test\''/'₩$4'/g' {} \;