bash 用 sed 将 old-link-url 替换为 new-link-url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8544226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:11:05  来源:igfitidea点击:

replace old-link-url to new-link-url with sed

linuxbashshellseddebian

提问by Crazy_Bash

I'm writing a script in bash that would replace old-link-url to new-link-url my problem is that sed can't replace the url because of the slashes. If i put just some text it works.

我正在用 bash 编写一个脚本,它将替换 old-link-url 到 new-link-url 我的问题是 sed 由于斜线而无法替换 url。如果我只放一些文字就行了。

my code

我的代码

sed -e s/"$old_link"/"$new_link"/g wget2.html > playlist.txt

回答by Fredrik Pihl

sedsupports anycharacter as separator, so if the pattern you are trying to replace contains /, use a different separator. Most commonly used are #and |

sed支持任何字符作为分隔符,因此如果您尝试替换的模式包含/,请使用不同的分隔符。最常用的是#|

sed  's|foo|bar|g' input
sed  's#foo#bar#g' input

回答by jaypal singh

Don't forget to put double quotesif you are using variablesin sedsubstitution. Also, if your variable have /then use a different delimiter for sed. You can use _, %, |, #and many more.

double quotes如果您variablessed替代中使用,请不要忘记 put 。此外,如果您的变量有/然后使用不同的分隔符sed. 您可以使用_%|#等等。

So may be something like this would work -

所以可能像这样的事情会奏效 -

sed -e "s_"$old_link"_"$new_link"_g" wget2.html > playlist.txt