简体   繁体   English

bash脚本:查找并替换多字字符串

[英]bash scripting: find and replace multiword string

I'm trying to write a bash script to configure a server and I need to change the line: 我正在尝试编写bash脚本来配置服务器,并且需要更改以下行:

    listen = /var/run/php5-fpm.sock

to equal the following: 等于以下内容:

    listen = 127.0.0.1:9000

in the file: 在文件中:

    /etc/php5/fpm/pool.d/www.conf

So I've been looking at tutorials for using sed and I've tried the following command to no avail: 因此,我一直在寻找使用sed的教程,并且尝试了以下命令,但无济于事:

    $~: sed -i 's//var/run/php5-fpm.sock/127.0.0.1:9000/g' /etc/php5/fpm/pool.d/www.conf
    $~: sed: -e expression #1, char 8: unknown option to `s'

I've tried escaping the forward slash with a backslash: '/' but I think I'm on the wrong track. 我尝试用反斜杠转义正斜杠:“ /”,但我认为我走错了路。 There must be a better way to do this? 必须有更好的方法来做到这一点?

Thanks for your help. 谢谢你的帮助。

This is because you are trying to replace the character '/' in the pattern, and this character is used to delimit the 's///' expression. 这是因为您试图替换模式中的字符'/',并且该字符用于分隔's ///'表达式。 You have two choices, you can escape every '/' character with '/'or - and this is the one I prefer, use a different character to delimit the pattern and replacement string - I tend to use '!' 您有两种选择,可以用'/'或-来转义每个'/'字符,这是我更喜欢的一个,使用其他字符来分隔模式和替换字符串-我倾向于使用'!'

The character immediately after the 's' is used to delimit the expressions. 紧跟在“ s”之后的字符用于分隔表达式。

sed -i 's!/var/run/php5-fpm.sock!127.0.0.1:9000!g' /etc/php5/fpm/pool.d/www.conf

I actually have got into the habit of ALWAYS using '!' 实际上,我已经养成了总是使用'​​!'的习惯。 for sed, and perl - as you end up having to escape less characters and ultimately save time. sed和perl-最终不得不逃脱更少的字符并最终节省时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM