sed 就地标志,适用于 Mac (BSD) 和 Linux [英] sed in-place flag that works both on Mac (BSD) and Linux

查看:29
本文介绍了sed 就地标志,适用于 Mac (BSD) 和 Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Linux 和 Mac 上调用 sed 在没有备份的情况下进行就地编辑?虽然 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆