开发者

Is there a way to use regexes in sed without having to put backslash everywhere?

开发者 https://www.devze.com 2023-01-26 16:20 出处:网络
To use regex syntax in sed, you have to put in \\ before (, {, etc. to use them as special characters. For example:

To use regex syntax in sed, you have to put in \ before (, {, etc. to use them as special characters. For example:

~ > echo 123 | sed 's/[开发者_如何学JAVA0-9]{2}/x/'
123

vs.

~ > echo 123 | sed 's/[0-9]\{2\}/x/'
x3

This is the reverse of what I'm used to. Is there any way to make characters have special meanings by default?


Try:

echo 123 | sed -r 's/[0-9]{2}/x/'


If your sed doesn't have -r:

echo 123 | perl -pe 's/[0-9]{2}/x/'
0

精彩评论

暂无评论...
验证码 换一张
取 消