GNU sed 和 BSD sed 区别说明
我编写了以下命令,
echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/'
期望得到以下输出
uno
这确实是我的 GNU Sed 4.8 的情况。
但是,我已经验证 BSD Sed 输出
为什么会这样?
I wrote the following command
echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/'
expecting the following output
uno
This is indeed the case with my GNU Sed 4.8.
However, I've verified that BSD Sed outputs
Why is that the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说 BSD 的 sed 仅兼容 POSIX。 POSIX 指定仅支持基本正则表达式,这有许多限制(例如,根本不支持 |(交替),不直接支持 + 和 ?)以及不同的转义要求。
BSD sed 是 MacOS 上的默认 sed,因此新系统上的第一件事就是获取 GNU 兼容的 sed:
brew install gsed
。I'd say that BSD's sed is POSIX-compatible only. POSIX specifies support only for basic regular expressions, which have many limitations (e.g., no support for | (alternation) at all, no direct support for + and ?) and different escaping requirements.
BSD sed is default one on MacOS so very first thing on a new system is to get GNU-compatible sed:
brew install gsed
.