如何在Mac OS上安装GNU grep?

14

我需要在我的Mac上安装GNU grep,但是我遇到了一些困难。

我尝试了这个方法:

brew install grep --with-default-names

但是自从Homebrew删除了--with-default-names选项,这已经不再是一个选择。

有人可以提供解决方案吗?

1个回答

22

是的,--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

你还应该看到我在开头提到的相同的“警告”信息。现在,默认情况下,Homebrew的grep会带有前缀“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,请遵循以下指示,在你的PATH开头添加/usr/local/opt/grep/libexec/gnubin。你必须在你的.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
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接