113

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.

smci
  • 32,567
  • 20
  • 113
  • 146
user3781201
  • 1,261
  • 2
  • 9
  • 7
  • 2
    Where did `brew` install GNU `sed`? Is that directory on your `PATH`? What do you get if you type `sed --version`? (BSD `sed` will complain; GNU `sed` will report its version information.) Until you either have the `brew` directory on your PATH ahead of `/usr/bin` or you learn to use the absolute pathname to GNU `sed`, you will not be running GNU `sed`. Be careful with quotes, too. `’` is a word-processing quote, unlike `'`. – Jonathan Leffler May 02 '15 at 16:02

11 Answers11

124

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.

gudok
  • 4,029
  • 2
  • 20
  • 30
Kashyap
  • 15,354
  • 13
  • 64
  • 103
  • 14
    --with-default-names option is no longer supported – Amol Jan 24 '19 at 21:25
  • 1
    Once --with-default-names is no longer supported, you can install it normally with `brew install gnu-sed` then get it's path with `brew info gnu-sed`. – Lucas Jul 12 '23 at 12:35
64

When you install the GNU version of sed for Mac OS X using:

$ brew install gnu-sed

The program that you use is gsed.

So for example:

$ echo "Calimero is a little chicken" > test
$ cat test
Calimero is a little chicken
$ gsed -i "s/little/big/g" test
$ cat test
Calimero is a big chicken

Also, to compliment the use of GNU command tools on Mac OS X, there is a nice blog post here to get the tools from linux:

Install and use GNU command line tools on Mac OS/OS X

giusti
  • 3,156
  • 3
  • 29
  • 44
anquegi
  • 11,125
  • 4
  • 51
  • 67
27

$ brew install gnu-sed

$ export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

With these two commands gnu-sed works properly

Supradeep
  • 3,246
  • 1
  • 14
  • 28
K Kishore
  • 271
  • 3
  • 2
21

The sed that ships with OS X is in /usr/bin.

The sed that homebrew installs is in /usr/local/bin.

If you prefer to use the homebrew one, you have two options:

Option 1

Every time you want to use homebrew sed, type

/usr/local/bin/sed

or, preferably

Option 2

Move /usr/local/bin/ ahead (i.e. before) /usr/bin in your PATH in your login profile, like this

 export PATH=/usr/local/bin:<other places>
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
9

If you need to use gnu-sed command with their normal names, you can add a "gnubin" directory to your PATH from your bashrc. Just use the following command in your bash or terminal.

export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
Tenzin Chemi
  • 5,101
  • 2
  • 27
  • 33
2

--with-default-names didn't work for me on Mac OS X 10.14.2 so I created a symlink named sed to gsed higher in the $PATH

I also created a symlink named sed.1 to the gsed.1 manpage higher in the $MANPATH so man would access the gsed manpage instead of the default sed manpage

1

this export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

is only valid per terminal SESSIOn as soon as you restart its GONE ( Mojave )

Georg
  • 11
  • 1
  • 2
    If you add it to `.bashrc` (if you use bash as your shell) or `.zshrc` if you're using zsh as your shell, it will be executed every time you log in. Apple used to default to `bash` as the shell, but in Catalina switched the default shell to `zsh`. You can tell which your account is set to use by running `w` in the terminal window - you'll get a bunch of lines, some of which (in my case, I use zsh) look like `jpb s000 - Sun20 1day -zsh` - the `-zsh` shows my account is using `zsh`. – Joe Block Apr 14 '21 at 13:07
  • Welcome to StackOverflow. Please note that this is not a forum and answers like this will typically be downvoted. (If you ever get a downvoted answer, you can just remove it; that's all that's required to restore your points.) Instead we are typically looking for answers that include a direct solution to the OP's problem, and what you did to get there. For more information, see [How to Answer](https://stackoverflow.com/help/how-to-answer). – Chaim Eliyah Oct 15 '21 at 20:15
0

Since the --with-default-names option was removed in Jan. 2019, my hack solution is:

# hack to override mac tools with homebrew versions (ls, sed, etc)
for p in `find "${HOMEBREW_PREFIX}" -type d -name gnubin` ; do
    export PATH="${p}:${PATH}"
done

which is a little slow (crawling the dir every login) but works without forcing me to modify my .bashrc for every gnu tool I happen to install with brew.

pjz
  • 41,842
  • 6
  • 48
  • 60
0

A slightly faster way to do what @pjz suggests is the following:

for p in $(ls -d ${HOMEBREW_PREFIX:-/usr/local}/Cellar/*/*/libexec/gnubin); do
    export PATH="${p}:${PATH}"
done

Of course this assumes every GNU package in brew will always have the same level of directories to get to gnubin.

Alternatively, you can speed up find by adding the -maxdepth 4 flag before -type d to reduce how far it has to do into directories.

0

When running brew install gnu-sed on latest homebrew it reports at the end:

==> Caveats
GNU "sed" has been installed as "gsed".
If you need to use it as "sed", you can add a "gnubin" directory
to your PATH from your bashrc like:

    PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"

gnu-sed installs by default as gsed. However, if you look in /opt/homebrew/opt/gnu-sed/libexec/gnubi you'll find a sed command. So following the above instructions to update the path should mean sed runs gnu-sed.

Andrew Coates
  • 1,775
  • 1
  • 10
  • 16
0

Once --with-default-names is no longer supported, you can install it normally with brew install gnu-sed and then get its path with brew info gnu-sed.

brew install gnu-sed
brew info gnu-sed

output:

...
==> Caveats
GNU "sed" has been installed as "gsed".
If you need to use it as "sed", you can add a "gnubin" directory
to your PATH from your bashrc like:

    PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
==> Analytics

...

Then use it in your path:

export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
Lucas
  • 1,514
  • 3
  • 16
  • 23