sed替换命令在Mac上不起作用 [英] sed replacement command not working on Mac
问题描述
我正在尝试使用sed用bash变量的值替换CMakelists.txt文件中的某些文本,但出现错误:
I'm trying to replace some text in a CMakelists.txt file with the value of a bash variable using sed, but I'm getting an error:
sed: 1: "'s/iPhone": invalid command code ?
sed命令:
sed -i "" 's/iPhone Developer/'$PROVPROF'/g' CMakelists.txt
PROVPROF始终具有以下格式的内容:
PROVPROF will always have something with this format:
iPhone Developer: Firstname Lastname (Numbers/Letters)
推荐答案
如果变量中的字符与用于s
的定界符相同,请尝试使用另一个定界符:
If you have characters in your variable which are the same as the delimiter you used to s
, try using another delimiter instead:
sed -i '' "s|iPhone Developer|$PROVPROF|g" CMakelists.txt
更罕见的事情:
sed -i '' $'s\xFFiPhone Developer\xFF'"$PROVPROF"$'\xFFg' CMakelists.txt
更新:
此外,请勿尝试将参数存储在变量中.分词并不总是能像您那样工作.这是错误的:
Update:
Also, don't try to store your arguments on a variable. Word splitting would not always work the way you do. This is wrong:
command="sed -i '' 's|iPhone Developer|$PROVPROF|g' CMakelists.txt"
$command
将出现类似sed: -e expression #1, char 1: unknown command: `''
的错误消息.在其他sed
上,消息可能有所不同.
Error message like sed: -e expression #1, char 1: unknown command: `''
would appear. On other sed
s the message may be different.
但是您可以使用数组:
command=(sed -i '' "s|iPhone Developer|$PROVPROF|g" CMakelists.txt)
"${command[@]}"
尽管如此,还是不值得赞扬的.如果可以直接运行,请直接运行.
It's still not commendable though. If you can run it directly, run it directly.
这篇关于sed替换命令在Mac上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!