如何逃脱Mac OS X上的加号(BSD)的sed? [英] How to escape plus sign on mac os x (BSD) sed?
问题描述
我试图查找和替换的字符出现一次或多次使用的sed在Mac上,从BSD常规命令sed的。
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 //摹
是足以取代所有O事件。
Using the /g
flag, s/o//g
is enough to replace all o occurrences.
为什么 +
不按预期:旧的,过时的RE +
是一个普通的字符(以及 |
,
?)。您应指定 -E
标志 SED
用现代常规EX pressions它做:
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
来源:人7 re_format
这篇关于如何逃脱Mac OS X上的加号(BSD)的sed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!