macOS Mojave(10.14.2)使用"sed -i"命令时报错'sed: 1: "xxx": extra characters at the end of l command"'

最近在 macOS Mojave(10.14.2)使用"sed -i"命令时报错,类似如下:

$ sed -i "s/^[ \t]*~AttributeList()[ \t]*LLVM_DELETED_FUNCTION;/~AttributeList() ;/g" llvm/tools/clang/include/clang/Sema/AttributeList.h
sed: 1: "llvm/tools/clang/includ ...": extra characters at the end of l command

刚刚遇到这个问题,百思不得其解。后来搜索了一下,才找到解释。

如下:

$ man 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.

...................................................

sed  -i 需要带一个字符串,用来备份源文件,这个字符串会加在源文件名后面组成备份的文件名。
如果这个字符串长度为 0,就是说是个空串,那么不备份。

$ sed -i "_nima" 's/xx/python/g' example.txt

这样 sed 不仅会修改文件,并且会生成一个 example.txt_nima 的备份文件。

如果不想备份修改前的内容,可以直接给个空参数,如下:

$ sed -i "" 's/xx/python/g' example.txt

所以,上面的命令,我们需要修改成如下方式:

$ sed -i "" "s/^[ \t]*~AttributeList()[ \t]*LLVM_DELETED_FUNCTION;/~AttributeList() ;/g" llvm/tools/clang/include/clang/Sema/AttributeList.h

蛮扯淡的。

参考链接


发布者

《macOS Mojave(10.14.2)使用"sed -i"命令时报错'sed: 1: "xxx": extra characters at the end of l command"'》上有1条评论

回复 *~* 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注