sedコマンドがMac OS Xで「未定義ラベル」エラーを表示する

Mac OS Xでsedコマンドが「undefined label」エラーをヒットする

次のシナリオを参照して、ファイルを作成し、コンテンツを追加し、検索して置換します。

$ touch testing.txt
$ echo "this is example.com" > testing.txt
$ cat testing.txt
this is example.com
$ sed -i 's/example/google/g' testing.txt
sed: 1: "testing.txt": undefined label 'esting.txt'

このsed -i 's/example/google/g' testing.txtコマンドはLinuxでは正しく機能しますが、Mac OS Xでは「undefined label」エラーメッセージが表示されます。

溶液

sedコマンドはMac OS Xでは少し異なります。「-i」オプションには、バックアップファイルに追加する拡張子を指示するパラメータが必要でした。

修正するには、バックアップファイルの拡張子を追加します(例: '.bak'):

$ sed -i '.bak' 's/example/google/g' testing.txt
$ ls -ls
8 -rw-r--r--  1 example  staff  19 Aug  2 14:22 testing.txt
8 -rw-r--r--  1 example  staff  19 Aug  2 14:21 testing.txt.bak
$ cat testing.txt
this is google.com
$ cat testing.txt.bak
this is example.com