MAC更新Sed

作者: LiHui 分类: Tool 发布时间: 2015-10-26 23:55

Sed突然不好使了,一个普通的替换都报莫名其妙的错误

原因是MAC上默认安装的sed是基于freebsd的,与Linux下用习惯的GNU版本还是有些差异的

解决方法:

先通过brew安装gnu-sed

~ on  master! ⌚ 23:39:37
$ brew install gnu-sed
==> Downloading http://ftpmirror.gnu.org/sed/sed-4.2.2.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/gnu-sed/4.2.2 --program-prefix=g
==> make install
==> Caveats
The command has been installed with the prefix "g".
If you do not want the prefix, install using the "with-default-names" option.

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/gnu-sed/libexec/gnubin:$PATH"

Additionally, you can access their man pages with normal names if you add
the "gnuman" directory to your MANPATH from your bashrc as well:

    MANPATH="/usr/local/opt/gnu-sed/libexec/gnuman:$MANPATH"

==> Summary
🍺  /usr/local/Cellar/gnu-sed/4.2.2: 9 files, 448K, built in 79 seconds

这时候,gsed会安装在目录/usr/local/Cellar/gnu-sed/4.2.2/bin/gsed,试了一下,好使

直接做个链接或者删除自带的sed居然都没权限

~ on  master! ⌚ 0:00:00
$ which gsed
/usr/local/bin/gsed

~ on  master! ⌚ 0:00:03
$ sudo ln -sf /usr/local/bin/gsed /usr/bin/sed
ln: /usr/bin/sed: Operation not permitted

~ on  master! ⌚ 0:00:07
$ sudo rm -f /usr/bin/sed
rm: /usr/bin/sed: Operation not permitted

看看PATH,为了直接用sed,只好直接重命名啦

~ on  master! ⌚ 0:00:50
$ echo $PATH
/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin

~ on  master! ⌚ 0:00:54
$ mv /usr/local/bin/gsed /usr/local/bin/sed

但是难以置信的是,默认还是会用/usr/bin/sed

不死心,想法切换root继续

$ sudo su
Password:
sh-3.2#
sh-3.2#
sh-3.2# rm -f /usr/bin/sed
rm: /usr/bin/sed: Operation not permitted

直接Finder,Command+G找到/usr/bin目录,然后删除sed

QQ20151027-0@2x.png

好吧,死心了,就用gsed吧,自己简直都要变成处女座了~!

记得以前没这问题,最近更新OS X的缘故?

纯属恶搞

浙ICP备16024533号

浙公网安备 33010802007459号