bash sed 命令在 mac 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9744300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed command not working in mac
提问by Nick Vanderbilt
Following sed command is not working on my lion mac.
遵循 sed 命令在我的 Lion mac 上不起作用。
find . -type f -exec sed -i 's/user_dashboard/user/g' {} \;
I am getting this error
我收到此错误
sed: 1: "./vendor/assets/javascr ...": invalid command code .
回答by shellter
The OSX version of sedis not the same as those found in most Linux systems.
的 OSX 版本与sed大多数 Linux 系统中的版本不同。
It extends the -ioption to give you the opportunity save a file with a different extension, but requires input for that extension.
它扩展了-i选项,让您有机会使用不同的扩展名保存文件,但需要输入该扩展名。
If you just want to overwrite the file in place, you need to use sed -i "" ...sedCmd.... fileNameto rename your file in-place.
如果您只想就地覆盖文件,则需要使用sed -i "" ...sedCmd.... fileName就地重命名文件。
Per @JamesMcMahon 's comment, see here for the full doc for OSX/BSD sed.
根据@JamesMcMahon 的评论,请参阅此处了解 OSX/BSD sed的完整文档。
I hope this helps.
我希望这有帮助。
回答by Eduardo Ivanec
-iprobably has a different meaning (not "in-place") in your version of sed. Try using gsedif available or replacing -iwith -eand using a temporary file (and a mvafterwards) to emulate it.
-i在您的 sed 版本中可能有不同的含义(不是“就地”)。尝试使用(gsed如果可用)或替换-i为-e并使用临时文件(以及mv之后的文件)来模拟它。
回答by zenin
replacing text inside a text file on the fly with sed on mac is possible.
在 mac 上用 sed 动态替换文本文件中的文本是可能的。
the command is just a little different.
命令只是有点不同。
with: -i , you specify an extension where sed will save the original file prior to the sed operation.
使用: -i ,您指定一个扩展名,其中 sed 将在 sed 操作之前保存原始文件。
run the command as:
运行命令为:
$ sed -i _bakup -E 's/THESTRING/THEGRANDSTRING' /tmp/jestinkt.txt
$ sed -i _bakup -E 's/THESTRING/THEGRANDSTRING' /tmp/jestinkt.txt

