How to use GNU sed on Mac OS 10.10+, 'brew install --default-names' no longer supported

user3781201 picture user3781201 · May 2, 2015 · Viewed 76.9k times · Source

Under Mac OS 10.10.3, I installed gnu-sed by typing:

brew install gnu-sed --default-names

When I type it again, I get the message:

gnu-sed-4.2.2 already installed

However, even after rebooting the system and restarting Terminal, I still cannot use the GNU version of sed. For example:

echo a | sed ’s_A_X_i’

returns: bad flag in substitution command 'i'

What should I do to get the GNU version working? Here are the paths in my $PATH variable.

/Users/WN/-myUnix
/opt/local/bin
/opt/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/Applications/calibre.app/Contents/MacOS
/opt/ImageMagick/bin
/usr/texbin 

I'm sorry if my question seems obvious, but I am learning shell scripting on my own and don't quite understand yet how UNIX programs are installed. Any help to use GNU compliant commands (in this case sed, but soon I'll need others as well) on my Mac without causing damage or unnecessary clutter would be greatly appreciated.

Answer

Kashyap picture Kashyap · Jan 15, 2016

Note (2019):

The --with-default-names option is removed since January 2019, so now that option is not available anymore.

When installing, Homebrew instructs on how to adapt the path, if one wants to use sed without the g prefix.


You already have the gnu-sed installed without the --with-default-names option.

  • With --with-default-names option it installs sed to /usr/local/bin/
  • Without that option it installs gsed

So in your case what you gotta do is:

$ brew uninstall gnu-sed
$ brew install gnu-sed --with-default-names

Update path if needed...

$ echo $PATH | grep -q '/usr/local/bin'; [ $? -ne 0 ] && export PATH=/usr/local/bin:$PATH
$ echo a | sed 's_A_X_i'

or use gsed as others suggested.