更新grep for Mac OS 10.7

时间:2011-12-02 05:38:39

标签: macos grep administration

我想将Mac上的grep更新为2.5.1以后的更新版本,该版本随Mac OS 10.7.2一起提供。我的问题是:更新Mac的grep(或任何类似程序)的最佳方法是什么?我可以使用Fink或MacPorts来安装新版本并设置我的路径变量以查看文件树的相应分支,或者我可以更新usr / bin中的grep程序,或者可能还有另一种我没有考虑过的方法。因为我对命令行和Mac的Unix后端相对较新,所以我担心会破坏某些东西。也就是说,我当然愿意从源代码编译最新的grep稳定版本并将其安装在/ usr / bin中,如果这是合适的方法。如果有人想知道我为什么要从2.5.1更新grep,我有两个原因:第一,我正在学习使用grep和基于2.5.3的参考书(可能类似,我知道);第二,更重要的是,我想学习如何更新这些程序只是为了有效地管理我自己的系统。

3 个答案:

答案 0 :(得分:5)

正如你所说,你可以使用Fink,MacPorts等......

但是如果你只是想更新grep,你可能想要获取源代码并进行编译。

如果您决定使用此选项,请勿将其安装在/ usr / bin

如果您这样做,您将覆盖操作系统所需的内容 因此,对于另一个版本,您可能会遇到问题,因为操作系统将会出现另一个版本。

而且,如果您这样做,在更新操作系统时会遇到问题,因为它可能会覆盖您自己的版本。

因此,如果要编译它,请将其放在/usr/local/bin(通常使用--prefix选项)中,并更新路径环境变量。
这是安全的方式。

通常,编译此类程序只是标准的./configuremakesudo make install内容。
但请务必首先查看编译选项,输入:

./configure --help

答案 1 :(得分:5)

以下是来自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

答案 2 :(得分:0)

最近变得更容易了:

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的任何地方。