sed就地标志,可在Mac(BSD)和Linux上使用

编程入门 行业动态 更新时间:2024-10-07 11:28:29
本文介绍了sed就地标志,可在Mac(BSD)和Linux上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否调用了sed以进行就地编辑而无需备份,而该备份在Linux和Mac上都可以使用?虽然OS X附带的BSD sed似乎需要sed -i '' …,但GNU sed Linux发行版通常随附将引号解释为空的输入文件名(而不是备份扩展名),而需要使用sed -i ….

Is there an invocation of sed todo in-place editing without backups that works both on Linux and Mac? While the BSD sed shipped with OS X seems to need sed -i '' …, the GNU sed Linux distributions usually come with interprets the quotes as empty input file name (instead of the backup extension), and needs sed -i … instead.

是否有任何两种语法都可以使用的命令行语法,所以我可以在两个系统上使用相同的脚本?

Is there any command line syntax which works with both flavors, so I can use the same script on both systems?

推荐答案

如果您真的只想简单地使用sed -i,那么以下方法在GNU和BSD/Mac sed上均可使用:

If you really want to just use sed -i the 'easy' way, the following DOES work on both GNU and BSD/Mac sed:

sed -i.bak 's/foo/bar/' filename

请注意缺少空格和点.

证明:

# GNU sed % sed --version | head -1 GNU sed version 4.2.1 % echo 'foo' > file % sed -i.bak 's/foo/bar/' ./file % ls file file.bak % cat ./file bar # BSD sed % sed --version 2>&1 | head -1 sed: illegal option -- - % echo 'foo' > file % sed -i.bak 's/foo/bar/' ./file % ls file file.bak % cat ./file bar

很显然,您可以删除.bak文件.

Obviously you could then just delete the .bak files.

更多推荐

sed就地标志,可在Mac(BSD)和Linux上使用

本文发布于:2023-11-24 14:11:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可在   标志   sed   Linux   BSD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!