LoginSignup
2
1

MacOSのsedコマンドが期待通りに動かないときの対処法

Last updated at Posted at 2023-12-25

事象

  • MacOSのsedコマンドでテキストファイルを編集しようとしたらエラーが発生。
% cat test.txt
hello world
% sed -i 's/hello/hi/g' test.txt
sed: 1: "test.txt": undefined label 'est.txt'

原因

  • MacOSに搭載されているのはBSD系のコマンドらしい。

対処法

  • brewでGNUのsedをインストールできる。
% brew install gnu-sed

または

% brew install gsed
  • aliasを作成しておくと便利かも。
.zshrc
alias sed='gsed'
  • 動作確認
% sed -i 's/hello/hi/g' test.txt
% cat test.txt
hi world
2
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
2
1