如何在Mac OS上安装GNU grep?



我需要在我的Mac上安装GNU grep,但我发现一些困难。

我尝试这样做:

brew install grep --with-default-names

但这不再是一种选择,因为 自制软件 删除了--with-default-names.

任何人都可以为此提供解决方案吗?

是的,--with-default-names已被删除。

但是一些公式,如grep,为此提供了一种解决方法:

$ brew info grep
...
==> Caveats
All commands have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
...

首先,要安装,只需执行install而无需--with-default-names

$ brew install grep
...
==> Summary
🍺  /usr/local/Cellar/grep/3.3: 21 files, 880.7KB

您还应该看到我在开头提到的相同"警告"信息。现在,默认情况下,Homebrewgrep将以"g"为前缀,因此可以作为ggrep访问。

$ ggrep -V
ggrep (GNU grep) 3.3
Packaged by Homebrew
Copyright (C) 2018 Free Software Foundation, Inc.
...

这可以防止它隐藏 Mac 附带的内置grep

$ grep -V
grep (BSD grep) 2.5.1-FreeBSD

如果您确实需要使用grep而不是ggrep,只需按照说明进行操作,然后将/usr/local/opt/grep/libexec/gnubin放在PATH的开头。您必须在.bashrc.bash_profile(无论您使用哪个(中执行此操作。

$ echo 'export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
$ grep -V
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
grep (GNU grep) 3.3
Packaged by Homebrew
...

最新更新