问题
I would like to update grep on my Mac to a more recent version than 2.5.1, which came with Mac OS 10.7.2. My question is: what is the best way to update grep (or any similar program) for the Mac? I could use Fink or MacPorts to install a new version and set my path variable to look in the appropriate branch of the file tree, or I could update the grep program in usr/bin, or perhaps there's another approach that I haven't considered. Because I am relatively new to the command line and the Unix back-end of the Mac, I'm concerned about breaking something. That said, I'd certainly be willing to compile the most recent stable release of grep from source and install it in /usr/bin if that's the appropriate method. In case anyone wonders why I'd want to update grep from 2.5.1, I have two reasons: 1st, I'm learning to use grep with a reference book that is based on 2.5.3 (probably similar, I know); 2nd and more importantly, I want to learn how to update such programs simply as a matter of administrating my own system effectively.
回答1:
As you said, you may use Fink, MacPorts, etc...
But if you just want to update grep, you may want to grab the sources, and compile them.
If you decide to go with this option, don't install it in /usr/bin.
If you do so, you will overwrite something needed by your OS.
So with another version, you may encounter problems, as the OS will except another version.
And also, if you do so, you'll have problems when updating your OS, as it might overwrite your own version.
So if you want to compile it, place it in /usr/local/bin
(usually with the --prefix
option), and update your path environment variable.
This is the safe way.
Usually, compiling such a program is just the standard ./configure
, make
and sudo make install
stuff.
But be sure to take a look at the compiling options first, by typing:
./configure --help
回答2:
The following is a very elegant solution from 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
回答3:
It's gotten easier recently:
brew install grep
Which causes lines like the following:
==> 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"
Then you would use:
ggrep --color=auto
Anywhere where you would have executed grep before.
来源:https://stackoverflow.com/questions/8352197/updating-grep-for-mac-os-10-7