sed替换错误:sed:-e expression#1,char 51:`s'的未知选项[duplicate]

时间:2022-03-24 06:06:32

Possible Duplicate:
replace URL using shell script

可能重复:使用shell脚本替换URL

I'm trying to replace the string in my php.ini configuration file : php.ini with sed. There are two options that i need to change with sed inside another script. Here's the sed command that i used:

我正在尝试用我的php.ini配置文件替换字符串:php.ini with sed。我需要在另一个脚本中使用sed更改两个选项。这是我使用的sed命令:

sed -i s/^session.save_handler.*/session.save_handler = memcache/ /etc/php5/fpm/php.ini
sed -i s/\;session.save_path.*/session.save_path = unix:/tmp/memcached.sock/ /etc/php5/fpm/php.ini

The first command run successfully, but the second one is not. Instead, it returns error:

第一个命令成功运行,但第二个命令不成功。相反,它返回错误:

sed: -e expression #1, char 51: unknown option to `s'

Any help would be greatly appreciated

任何帮助将不胜感激

2 个解决方案

#1


3  

You need to escape the / characters in the replacement side of your second example:

您需要转义第二个示例的替换方中的/字符:

sed -i s/\;session.save_path.*/session.save_path = unix:\/tmp\/memcached.sock/ /etc/php5/fpm/php.ini

It might be easier to understand if you just use a different delimiter, though:

但是,如果您只使用不同的分隔符,可能会更容易理解:

sed -i s@\;session.save_path.*@session.save_path = unix:/tmp/memcached.sock@ /etc/php5/fpm/php.ini

#2


0  

You've got slashes and spacesin the replacement text; it's going to be easiest to use a different character to mark the start and end of the substitute command sections, such as %, and to enclose the whole script in single quotes:

替换文本中有斜杠和空格;最简单的方法是使用不同的字符来标记替换命令部分的开头和结尾,例如%,并将整个脚本用单引号括起来:

sed -i 's%\;session.save_path.*%session.save_path = unix:/tmp/memcached.sock%' \
    /etc/php/fpm/php.ini

Note the single quotes around the progam; it is generally best to use them to avoid unexpected shell metacharacter expansions.

注意程序周围的单引号;通常最好使用它们来避免意外的shell元字符扩展。

#1


3  

You need to escape the / characters in the replacement side of your second example:

您需要转义第二个示例的替换方中的/字符:

sed -i s/\;session.save_path.*/session.save_path = unix:\/tmp\/memcached.sock/ /etc/php5/fpm/php.ini

It might be easier to understand if you just use a different delimiter, though:

但是,如果您只使用不同的分隔符,可能会更容易理解:

sed -i s@\;session.save_path.*@session.save_path = unix:/tmp/memcached.sock@ /etc/php5/fpm/php.ini

#2


0  

You've got slashes and spacesin the replacement text; it's going to be easiest to use a different character to mark the start and end of the substitute command sections, such as %, and to enclose the whole script in single quotes:

替换文本中有斜杠和空格;最简单的方法是使用不同的字符来标记替换命令部分的开头和结尾,例如%,并将整个脚本用单引号括起来:

sed -i 's%\;session.save_path.*%session.save_path = unix:/tmp/memcached.sock%' \
    /etc/php/fpm/php.ini

Note the single quotes around the progam; it is generally best to use them to avoid unexpected shell metacharacter expansions.

注意程序周围的单引号;通常最好使用它们来避免意外的shell元字符扩展。