![SED 无法处理复杂的字符串](https://linux22.com/image/706391/SED%20%E6%97%A0%E6%B3%95%E5%A4%84%E7%90%86%E5%A4%8D%E6%9D%82%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2.png)
我有一个文件,其开头如下:
[global]
pid = /var/run/php-fpm/php-fpm.pid
error_log = /var/log/php5-fpm.log
并想更换
error_log = /var/log/php5-fpm.log
和
error_log = /var/log/php-fpm/error.log
我想为此使用一个脚本并执行以下操作:
search='error_log = /var/log/php5-fpm.log'
replace='error_log = /var/log/php-fpm/error.log'
for file in `find -name 'php-fpm.conf'`; do
grep "$search" $file &> /dev/null
if [ $? -ne 0 ]; then
echo "Search string not found in $file!"
else
sed -i "s#$search#$replace#" $file
fi
done
这会引发以下错误:
sed: -e expression #1, char 0: no previous regular expression sed: -e expression #1, char 0: no previous regular expression