bash 如何在 Mac OS 10.10+ 上使用 GNU sed,不再支持“brew install --default-names”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30003570/
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
How to use GNU sed on Mac OS 10.10+, 'brew install --default-names' no longer supported
提问by user3781201
Under Mac OS 10.10.3, I installed gnu-sed by typing:
在 Mac OS 10.10.3 下,我通过键入以下内容安装了 gnu-sed:
brew install gnu-sed --default-names
When I type it again, I get the message:
当我再次输入时,我收到消息:
gnu-sed-4.2.2 already installed
gnu-sed-4.2.2 已经安装
However, even after rebooting the system and restarting Terminal, I still cannot use the GNU version of sed. For example:
但是,即使在重新启动系统并重新启动终端后,我仍然无法使用 sed 的 GNU 版本。例如:
echo a |?sed 's_A_X_i'
returns: bad flag in substitution command 'i'
返回:替换命令“i”中的错误标志
What should I do to get the GNU version working? Here are the paths in my $PATH variable.
我应该怎么做才能使 GNU 版本正常工作?这是我的 $PATH 变量中的路径。
/Users/WN/-myUnix
/opt/local/bin
/opt/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/Applications/calibre.app/Contents/MacOS
/opt/ImageMagick/bin
/usr/texbin
I'm sorry if my question seems obvious, but I am learning shell scripting on my own and don't quite understand yet how UNIX programs are installed. Any help to use GNU compliant commands (in this case sed, but soon I'll need others as well) on my Mac without causing damage or unnecessary clutter would be greatly appreciated.
如果我的问题看起来很明显,我很抱歉,但我正在自己学习 shell 脚本并且不太了解 UNIX 程序是如何安装的。在我的 Mac 上使用 GNU 兼容命令(在这种情况下为 sed,但很快我也需要其他命令)而不会造成损坏或不必要的混乱的任何帮助将不胜感激。
回答by Kashyap
Note (2019):
注(2019):
The --with-default-names
option is removedsince January 2019, so now that option is not available anymore.
该--with-default-names
选项自 2019 年 1 月起被删除,因此现在该选项不再可用。
When installing, Homebrew instructs on how to adapt the path, if one wants to use sed without the g
prefix.
安装时,Homebrew 会指示如何调整路径,如果您想使用不带g
前缀的sed 。
You already have the gnu-sed installed without the --with-default-names
option.
您已经安装了没有该--with-default-names
选项的 gnu-sed 。
- With
--with-default-names
option it installssed
to/usr/local/bin/
- Without that option it installs
gsed
- 使用
--with-default-names
选项安装sed
到/usr/local/bin/
- 如果没有该选项,它会安装
gsed
So in your case what you gotta do is:
所以在你的情况下你要做的是:
$ brew uninstall gnu-sed
$ brew install gnu-sed --with-default-names
Update path if needed...
如果需要,更新路径...
$ echo $PATH | grep -q '/usr/local/bin'; [ $? -ne 0 ] && export PATH=/usr/local/bin:$PATH
$ echo a | sed 's_A_X_i'
or use gsed
as others suggested.
或gsed
按照其他人的建议使用。
回答by anquegi
When you install the GNU version of sed
for Mac OS X using:
当您使用以下命令安装sed
Mac OS X的 GNU 版本时:
$ brew install gnu-sed
The program that you use is gsed
.
您使用的程序是gsed
.
So for example:
例如:
$ echo "Calimero is a little chicken" > test
$ cat test
Calimero is a little chicken
$ gsed -i "s/little/big/g" test
$ cat test
Calimero is a big chicken
Also, to compliment the use of GNU command tools on Mac OS X, there is a nice blog post here to get the tools from linux:
此外,为了赞美在 Mac OS X 上使用 GNU 命令工具,这里有一篇不错的博客文章可以从 linux 获取这些工具:
回答by Mark Setchell
The sed
that ships with OS X is in /usr/bin
.
该sed
附带OS X中/usr/bin
。
The sed
that homebrew
installs is in /usr/local/bin
.
在sed
这homebrew
安装在/usr/local/bin
。
If you prefer to use the homebrew
one, you have two options:
如果您更喜欢使用其中homebrew
一种,您有两种选择:
Option 1
选项1
Every time you want to use homebrew
sed
, type
每次要使用时homebrew
sed
,请键入
/usr/local/bin/sed
or, preferably
或者,最好
Option 2
选项 2
Move /usr/local/bin/
ahead (i.e. before) /usr/bin
in your PATH in your login profile, like this
移动/usr/local/bin/
提前(即前)/usr/bin
在PATH中的登录配置,这样
export PATH=/usr/local/bin:<other places>
回答by K Kishore
$ brew install gnu-sed
$ brew install gnu-sed
$ export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
With these two commands gnu-sed works properly
使用这两个命令 gnu-sed 可以正常工作
回答by imechemi
If you need to use gnu-sed command with their normal names, you can add a "gnubin" directory to your PATH from your bashrc. Just use the following command in your bash or terminal.
如果您需要使用具有正常名称的 gnu-sed 命令,您可以从您的 bashrc 向您的 PATH 添加一个“gnubin”目录。只需在 bash 或终端中使用以下命令。
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
回答by chipsandslasa
--with-default-names
didn't work for me on Mac OS X 10.14.2 so I created a symlink named sed
to gsed
higher in the $PATH
--with-default-names
没有对我来说有效在Mac OS X 10.14.2所以我创建了一个名为符号链接sed
到gsed
在较高$PATH
I also created a symlink named sed.1
to the gsed.1
manpage higher in the $MANPATH
so man
would access the gsed
manpage instead of the default sed
manpage
我还创建了一个符号链接,命名sed.1
为gsed.1
更高的联机帮助页,$MANPATH
以便man
访问gsed
联机帮助页而不是默认sed
联机帮助页