事象
- 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