简体   繁体   English

为什么这个匹配数字的sed命令不起作用?

[英]Why does this sed command to match number not work?

My command is like this: 我的命令是这样的:

echo "12 cats" | sed 's/[0-9]+/Number/g'

(I'm using the sed in vanilla Mac) (我在香草Mac中使用sed

I expect the result to be: 我希望结果如下:

Number cats

However, the real result is: 但是,真正的结果是:

12 cats

Does anyone have ideas about this? 有没有人有这个想法? Thanks! 谢谢!

+必须被反击以获得其特殊含义。

echo "12 cats" | sed 's/[0-9]\+/Number/g'

Expanding the + modifier works for me: 扩展+修饰符对我有用:

echo "12 cats" | sed 's/[0-9][0-9]*/Number/g'

Also, the -E switch would make the + modifier work, see choroba's answer. 此外, -E开关会使+修饰符起作用,请参阅choroba的答案。

使用perl,这将适用于unix或linux。

echo "12 cats" | perl -pe 's/\d+/Number/g'

In a very mystic way that is not explained (at least in resources I have so far..), You should check up the next terms: 以非常神秘的方式解释(至少在我到目前为止的资源中......),你应该检查下一个术语:

globbing (in the context of bash), regex, extended regex globbing(在bash的上下文中),正则表达式,扩展的正则表达式

For your question it seems that the expr' you gave to sed is a regex or extended regex....so by a tutorial I read you should insert also the -r before your actual command in sed... 对于你的问题,似乎你给sed的expr是一个正则表达式或扩展的正则表达式....所以通过我读过的教程你应该在sed中的实际命令之前插入-r ...

echo "12 cats" | 回声“12只猫”| sed -r 's/[0-9]+/Number/g' sed -r's / [0-9] + / Number / g'

Works for me in an Ubuntu 16.04 , bash. 在Ubuntu 16.04中为我工作,bash。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM