Installed GNU grep on OSX, but can't use

主宰稳场 提交于 2019-12-07 01:10:45

问题


I've tried installing GNU grep on OSX, and it seems to be installed, but I can't use it.. I've done so using homebrew, Macports is having some issues currently, so I can't use that.

To install: brew tap homebrew/dupes; brew install grep

Which returns: Warning: homebrew/dupes already tapped! Warning: homebrew/dupes/grep-2.21 already installed

Symlinking seems to work to /usr/local/bin/ggrep. When I add the alias alias grep="ggrep" and do grep --version, I get -bash: ggrep: command not found. Which is true, since there is no ggrep in the folder. I've tried installing with and without --with-default-names.

The folder /usr/local/Cellar/grep/2.21/bin/ contains the following:

-r-xr-xr-x 1 Wes admin 158 Oct 14 09:27 egrep
-r-xr-xr-x 1 Wes admin 158 Oct 14 09:27 fgrep

Which is strange to me, since the documentation implies that The command has been installed with the prefix "g".

I've seen the following post, but none of the solutions work for me. Updating grep for Mac OS 10.7

Does anyone have any solutions? I really want to use GNU grep.

Output of brew unlink grep && brew link grep -v:

Unlinking /usr/local/Cellar/grep/2.21...
6 symlinks removed
Linking /usr/local/Cellar/grep/2.21...
ln -s ../Cellar/grep/2.21/bin/egrep egrep
ln -s ../Cellar/grep/2.21/bin/fgrep fgrep
ln -s ../../Cellar/grep/2.21/share/info/grep.info grep.info info /usr/local/share/info/grep.info
ln -s ../../../Cellar/grep/2.21/share/man/man1/egrep.1 egrep.1
ln -s ../../../Cellar/grep/2.21/share/man/man1/fgrep.1 fgrep.1
ln -s ../../../Cellar/grep/2.21/share/man/man1/grep.1 grep.1
6 symlinks created`

New: brew uninstall grep; brew install grep

$ which -a grep
/usr/bin/grep

$ which -a ggrep
/usr/local/bin/ggrep

$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin

This time, it seems something is different. ggrep is finally installed! I think the unlink/link straightened some things out. How can I set ggrep as the default? With alias?


回答1:


To make GNU grep the default install it with --with-default-names:

$ brew install grep --with-default-names

If you already have it installed use reinstall instead of install.

Ensure that /usr/local/bin (the location of GNU grep) is before /usr/bin (the location of the BSD grep) in your $PATH; which seems to be the case here.

You might have to start a new shell session afterward because Bash caches the binaries paths for the current session. This means that the first time you use grep it’ll determine which binary it’ll use depending on your $PATH and cache it. The next time it’ll use the cached value so changing your $PATH won’t change anything until you reload the shell.




回答2:


Offically out of date for the answer above.

As of Homebrew version 2.0.0 the --with-default-names flag is no longer available.

from the official documentation

--with-default-names is no longer supported. It is now installed into its own directory and you will need to adjust your PATH to use it.

What you need to do is to add this command to your shell

PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"


来源:https://stackoverflow.com/questions/33231370/installed-gnu-grep-on-osx-but-cant-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!