mac のsed のiオプションがうまくいかないのを解決する

mac のsed はlinux のsed と動作が違うようだ。
主にiオプションで置換したいとき、うまくいかない。

% sed -i 's;oranie;orange;' /tmp/fluit.txt
sed: 1: "/tmp/fluit.txt": invalid command code f

実は、iオプションを使用するときは後ろに"" をつけてやるとうまくいく。
mac で oranie をorange に置換する例。

# 置換前
% cat /tmp/fruit.txt
oranie
apple

# iオプションの後ろに""をつける
% sed -i "" 's;oranie;orange;' /tmp/fruit.txt

# 確認
% cat /tmp/fruit.txt
orange
apple

なお、ubuntu だと"" は不要。

# 置換前
$ cat /tmp/fruit.txt
oranie
apple

# iオプションの後ろに何も付けなくて良い
$ sed -i 's;oranie;orange;' /tmp/fruit.txt

# 確認
$ cat /tmp/fruit.txt
orange
apple

http://arshavin0909.hateblo.jp/entry/2012/08/29/150609