如何在 mac os x (BSD) sed 上转义加号? [英] How to escape plus sign on mac os x (BSD) sed?

查看:16
本文介绍了如何在 mac os x (BSD) sed 上转义加号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 mac 上使用 sed 查找和替换一个或多个字符,来自 BSD 通用命令.

I'm trying to find and replace one or more occurrences of a character using sed on a mac, sed from the BSD General Commands.

我尝试:

echo "foobar" | sed -e "s/o+//g

期待看到:

fbar

但我看到了

foobar

我当然可以手动扩展加号:

I can of course just expand the plus manually with:

echo "foobar" | sed -e "s/oo*//g"

但是我该怎么做才能使加号起作用?

but what do I have to do to get the plus sign working?

推荐答案

使用 /g 标志,s/o//g 足以替换所有 o 次出现.

Using the /g flag, s/o//g is enough to replace all o occurrences.

为什么 + 不能按预期工作: 在旧的、过时的重新 + 是一个普通字符(以及 |, ?).您应该为 sed 指定 -E 标志以使其使用现代正则表达式:

Why + doesn't work as expected: in old, obsolete re + is an ordinary character (as well as |, ?). You should specify -E flag to sed to make it using modern regular expressions:

echo "foobar" | sed -E -e "s/o+//"
# fbar

来源:man 7 re_format.

这篇关于如何在 mac os x (BSD) sed 上转义加号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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