• Twitter
  • rss
  • facebook

緊隨 ROOTLAU.COM 的最新消息和資訊

在 Linux 的 shell 上搜尋文字並替代

Comments Off

分類 : I.T., Linux

今天真的忙過死去活來!在決定放棄使用 Pacific Internet 的寬頻而改用 PCCW 的一刻,已經知道會有這一天的來臨‧‧‧ 無耐 Pacific Internet 實在不爭氣,連我這個 Reseller Partner 也要‧‧‧

接著就是要將我的 Servers (幾部) 的網絡設定,DNS,HTTPD,SENDMAIL,‧‧‧ 全部也要將 IP 更換下來,痛苦‧‧‧

幸好徙網路上找到了一個可以將文字更換的程式,工作就可以比較輕鬆地完成!

#!/bin/bash
#       This script will search and replace all regular files for a string
#       supplied by the user and replace it with another string.
#
#       Written by Daniel McCarthy
# daniel.mccarthy@linuxphile.org
#
function usage {
echo “”
echo “Search/replace script”
echo ”    Written by Daniel McCarthy”
echo ” daniel.mccarthy@linuxphile.org”
echo ” http ://linuxphile.org”
echo “”
echo “Not enough parameters provided.”
echo “Usage: ./$0 searchstring replacestring”
echo “Remember to escape any special characters in the searchstring or the replacestring”
echo “”
}

#check for required parameters
if  [ ${ #1 } -gt 0  ]  &&  [ ${ #2 } -gt 0  ];
then
for f in `find  -type f`;
do
if grep -q $1 $f;
then
cp $f $f.bak
echo “The string $1 will be replaced with $2 in $f”
sed s/$1/$2/g < $f.bak > $f
rm $f.bak
fi
done

else
#print usage informamtion
usage
fi

參考:http://linuxphile.org/node/13

Share to Google Plus

Comments are closed.