bash Sed 错误:替代命令中的错误标志:'U'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32610261/
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 13:34:24  来源:igfitidea点击:

Sed error: bad flag in substitute command: 'U'

regexbashsed

提问by JasmineOT

I'm new in bash script and trying to replace some words in my file using sed. Following is the bash I use in my script:

我是 bash 脚本的新手,并尝试使用 sed 替换文件中的一些单词。以下是我在脚本中使用的 bash:

sed -i '' "s/<pre>.*<\/pre>/<pre>($NEWNAME) $MD5<\/pre>/"~/Desktop/replace.html

And I got error message saying: bad flag in substitute command: 'U'. I use double quote because I need to put variables in.

我收到错误消息说:bad flag in substitute command: 'U'。我使用双引号是因为我需要放入变量。

My environment is Mac.

我的环境是Mac。

======================================

======================================

1.Turns out I forgot to leave a space between replace string and file name. Which led to the result always showing: bad flag in substitute command: '~'. It works now.

1.原来我忘记在替换字符串和文件名之间留一个空格。这导致结果总是显示:bad flag in substitute command: '~'。它现在有效。

2.The reason is I used MD5=$(md5 path)to create MD5 value which gets the reault of MD5 (path) *****, and the path contains /which breaks the regex. After changing MD5=$(md5 -q path), it will be ok.

2.原因是我用来MD5=$(md5 path)创建MD5值,它得到reault MD5 (path) *****,并且路径包含/打破正则表达式。改MD5=$(md5 -q path)好后就好了。

回答by anubhava

Most likely your $NEWNAMEvariable has a forward slash in it, which is being used as regex delimiter in sed. Try this sedwith an alternate delimitere.g. ~:

很可能您的$NEWNAME变量中有一个正斜杠,它被用作sed. 尝试sed使用备用分隔符,例如~

sed -i '' "s~<pre>.*</pre>~<pre>($NEWNAME) $MD5</pre>~" ~/Desktop/replace.html