简体   繁体   中英

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)

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.

使用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

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...

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

Works for me in an Ubuntu 16.04 , bash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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