bash sed 替换命令在 Mac 上不起作用

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

sed replacement command not working on Mac

regexmacosbashsed

提问by Otto45

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 用 bash 变量的值替换 CMakelists.txt 文件中的一些文本,但出现错误:

sed: 1: "'s/iPhone": invalid command code ?

sed command:

sed 命令:

sed -i "" 's/iPhone Developer/'$PROVPROF'/g' CMakelists.txt

PROVPROF will always have something with this format:

PROVPROF 将始终具有以下格式:

iPhone Developer: Firstname Lastname (Numbers/Letters)

回答by konsolebox

If you have characters in your variable which are the same as the delimiter you used to s, try using another delimiter instead:

如果您的变量中有与您以前s使用的分隔符相同的字符,请尝试使用另一个分隔符:

sed -i '' "s|iPhone Developer|$PROVPROF|g" CMakelists.txt

Something more rare:

更罕见的东西:

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

Error message like sed: -e expression #1, char 1: unknown command: `''would appear. On other seds the message may be different.

sed: -e expression #1, char 1: unknown command: `''会出现类似的错误信息。在其他seds 上,消息可能会有所不同。

But you can use an array:

但是你可以使用一个数组:

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.

不过还是不敢恭维。如果可以直接运行,就直接运行。

回答by mohamed chalal

The reason this doesn't work is because sed on linux is different that sed on mac. In order for your commands to work as intended, I recommend installing gnu-sed through homebrew.

这不起作用的原因是因为 linux 上的 sed 与 mac 上的 sed 不同。为了让您的命令按预期工作,我建议通过自制软件安装 gnu-sed。

You can do this via: brew install gnu-sed --with-default-names

您可以通过以下方式执行此操作: brew install gnu-sed --with-default-names

NOTE: --with-default-namesflag will attempt to symlink gnu-sed with your sed, should be what you want but if not, I warned you!

注意:--with-default-namesflag 将尝试将 gnu-sed 与您的 sed 进行符号链接,这应该是您想要的,但如果不是,我警告过您!

then run hash -ron bash, or rehashon zsh.

然后hash -r在 bash 或rehashzsh 上运行。

If it does not work after that, you must manually symlink gnu-sed with sed via:

如果之后它不起作用,您必须通过以下方式手动符号链接 gnu-sed 和 sed:

ln -s /usr/local/bin/gsed $(which sed)

ln -s /usr/local/bin/gsed $(which sed)

回答by Jan Nielsen

With file.txtcontaining:

随着file.txt含:

Angelina used to like iPhone Developers.
Now she's more of an Android Developer fan-girl.

and Bash script like:

和 Bash 脚本,如:

#!/bin/bash
#
#
export MONKEY_1="Billy Bob";
export MONKEY_2="Brad Pitt";

sed -e "s/iPhone Developer/$MONKEY_1/g" -e "s/Android Developer/$MONKEY_2/g" file.txt

running it yields:

运行它会产生:

Angelina used to like Billy Bobs.
Now she's more of an Brad Pitt fan-girl.