简体   繁体   English

我如何使用〜作为分隔符使用sed查找和替换文本

[英]How do I find and replace text using sed using ~ as delimiter

Good-day, 美好的一天,

In a Bash shell script I'm putting together, I am trying to find this text: /usr/local/freeswitch/log/freeswitch.log and replace it with: /var/log/freeswitch/freeswitch.log in this file: /etc/fail2ban/jail.local 在我整理的Bash shell脚本中,我试图找到以下文本: /usr/local/freeswitch/log/freeswitch.log并将其替换为:在该文件中的/var/log/freeswitch/freeswitch.log/etc/fail2ban/jail.local

This is what I have tried so far, both of which result in the error: sed: -e expression #1, char 75: unterminated `s' command 到目前为止,这是我尝试过的操作,都导致错误: sed:-e表达式#1,字符75:未终止的s命令

Attempt #1 尝试#1

sed -i 's~usr/local/freeswitch/log/freeswitch.log~var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local

Attempt #2 尝试#2

sed -i 's~usr/local/freeswitch/log/freeswitch.log/var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local

My research shows that since the text I'm searching for includes the "/" character, I should be using a different delimiter "~" to separate the find and replace strings. 我的研究表明,由于要搜索的文本包含“ /”字符,因此我应该使用其他定界符“〜”来分隔查找和替换字符串。 But looks like I'm doing something wrong, any assistance would be appreciated, thanks. 但是看起来我做错了,感谢您的任何帮助。

The structure of a sed substitution command is s/PATTERN/REPLACEMENT/ (note the delimiter at the end of the command). sed替换命令的结构为s/PATTERN/REPLACEMENT/ (请注意命令末尾的定界符)。

You're right, you can change the delimiter to a different character, so if you're going to use ~ you need to put one of those at the end of the command. 没错,您可以将定界符更改为其他字符,因此,如果要使用~ ,则需要在命令末尾放置其中一个。

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

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