We know that the sed on Mac OS is the POSIX sed, which cannot use many options. On the other hand, GNU sed is very convenient. For example, GNU sed interprets escape sequences like \t
, \n
, \001
, \x01
, \w
, and \b
. OSX's sed and POSIX sed only interpret \n
(but not in the replacement part of s
). See Differences between sed on Mac OSX and other “standard” sed? for more detail.
Another thing is that the --with-default-names
option is removed since January 2019, so now we cannot use brew install gnu-sed --with-default-names
to replace the sed directly.
In this post, we will replace the OSX’s sed with GNU sed and set it as default.
Install
We first confirm the path of sed and install GNU sed with brew.
$ which sed
/usr/bin/sed$ brew install gnu-sed
Next, we use brew info
to see the GNU sed information.
$ brew info gnu-sed
...
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"
...