为 Mac OS 10.7 更新 grep

4
我想将我的Mac上的grep更新到比10.7.2随附的2.5.1版本更高的版本。我的问题是:如何最好地为Mac更新grep(或任何类似程序)?我可以使用Fink或MacPorts安装新版本并设置我的路径变量以查找文件树的适当分支,也可以更新usr / bin中的grep程序,或者可能还有其他我没有考虑的方法。因为我对命令行和Mac的Unix后端相对较新,所以我担心会出现问题。话虽如此,如果那是适当的方法,我肯定愿意从源代码编译最新的稳定版本,并在/usr/bin中安装它。如果有人想知道为什么我要从2.5.1更新grep,我有两个理由:第一,我正在学习使用基于2.5.3的参考书的grep(可能相似,我知道);第二,更重要的是,我想学习如何简单地更新这些程序,作为有效管理自己系统的一个方面。

更适合使用superuser.com或apple.stackexchange.com(他们可能会提到fink或macports:http://stackoverflow.com/questions/20853/macports-or-fink)。 - Thilo
3个回答

6
如你所说,你可以使用Fink、MacPorts等工具。但如果你只想更新grep,你可能需要获取源代码并进行编译。
如果你选择这个选项,请勿将其安装在/usr/bin下。如果这样做,你会覆盖操作系统需要的某些内容。因此,你可能会遇到问题,因为操作系统需要另一个版本。
而且,如果你这样做,当你更新操作系统时,你可能会遇到问题,因为它可能会覆盖你自己的版本。
因此,如果你想编译它,请将它放在/usr/local/bin中(通常使用--prefix选项),并更新你的路径环境变量。这是安全的方式。
通常,编译这样的程序只需要标准的./configure、make和sudo make install就可以了。但首先要看一下编译选项,输入:
./configure --help

4
дёҚиҰҒжӣҝжҚўж“ҚдҪңзі»з»ҹзҡ„grepе‘Ҫд»ӨгҖӮ~/binжҲ–~/local/binжҳҜеҸҰдёҖдёӘдёҚй”ҷзҡ„йҖүжӢ©гҖӮ - Thilo
@Macmade,我喜欢在/usr/local/bin中从源代码编译的选项。我应该在哪一步使用--prefix选项?一旦我知道了这一点,我会阅读命令文档并尝试一下。谢谢。 - Gregory
1
--prefix 在配置编译时使用。基本上:./configure --prefix=/usr/local [OTHER OPTIONS],然后 make,最后 sudo make install - Macmade
1
请注意,前缀仅为/usr/local。安装将在子目录bin(因此为/usr/local/bin)中放置二进制文件,在子目录/lib中放置库等... - Macmade
@Macmade,您的指示似乎完美无缺。我阅读了./configure --help,它表明默认情况下会安装在/usr/local,但是我按照您的建议使用了--prefix参数来指定安装位置,以确保安全。 - Gregory

6

最近变得更容易了:

brew install grep

这导致了以下类似的代码行:

==> Installing dependencies for grep: pcre
==> Installing grep dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.43.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pcre-8.43.high_sierra.bottle.tar.gz
  /usr/local/Cellar/pcre/8.43: 204 files, 5.5MB
==> Installing grep
==> Downloading https://homebrew.bintray.com/bottles/grep-3.3.high_sierra.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring grep-3.3.high_sierra.bottle.2.tar.gz
==> 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"
==> Summary
  /usr/local/Cellar/grep/3.3: 21 files, 880.7KB
==> Caveats
==> grep
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"

那么你可以使用:

ggrep --color=auto

无论哪里你以前用过grep的地方。


6

以下是非常优雅的解决方案,来源于http://www.heystephenwood.com/2013/09/install-gnu-grep-on-mac-osx.html

# Enable dupe and install
brew tap homebrew/dupes
brew install homebrew/dupes/grep
# Install the perl compatible regular expression library

brew install pcre

# Add the symlink to a place in $PATH
ln -s /usr/local/Cellar/grep/2.14/bin/ggrep /usr/bin/ggrep
# Add an alias
alias grep="ggrep"

# Verify you got it!
$ grep --version

grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
# If you want it to be permanent, you can add the alias line to your ~/.bash_profile
# You probably want the alias to stick after reboots
echo 'alias grep="ggrep"' >> ~/.bash_profile

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