LoginSignup
0
1

More than 5 years have passed since last update.

Mac環境でsedコマンドを使う時の注意点

Posted at

MacとCentOS(Ubuntuも?)でsedコマンドの-iオプションの挙動が異なります。
どう違うかなどは以下のmanコマンドで出力されるマニュアルを見てみましょう。

Macのsedコマンド

-i extension
             Edit files in-place, saving backups with the specified extension.  If a zero-length extension is
             given, no backup will be saved.  It is not recommended to give a zero-length extension when in-place
             editing files, as you risk corruption or partial content in situations where disk space is exhausted,
             etc.

CentOSのsedコマンド

-i[SUFFIX], --in-place[=SUFFIX]
              edit files in place (makes backup if extension supplied).  The default operation mode is to break symbolic and
              hard links.  This can be changed with --follow-symlinks and --copy.

ざっくり言うとMacの方はバックアップ用のファイルを作成することが推奨されているため、-iの後に何も記述しないとエラーになります。
よって、Macでsedコマンドを良い感じに使いたいなら、brew installしましょう。
そして、.bashrcにエイリアスを設定しましょう。

$ brew install gnu-sed
$ vim ~/.bashrc
alias sed='gsed'
$ source ~/.bashrc

これで、-iオプションの際にバックアップ用のファイルを指定しないで済みます。(=エラーがなくなる)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1