替换正则表达式不适用于sed(Replace regex does not work with sed)
我试图替换此末尾的数字: version=12在文件config.file
这是我的脚本:
nbr=13 sed -re "s/version=\d+/$nbr/g" config.file正则表达式似乎没有检测到数字。
有人有什么想法吗?
I am trying to replace the number at the end of this : version=12 containted in the file config.file
Here is my script :
nbr=13 sed -re "s/version=\d+/$nbr/g" config.fileThe regex does not seem to detect the number.
Have someone any ideas why ?
最满意答案
您可以使用
nbr=13 sed -re "s/(version=)[0-9]+/\1$nbr/g" config.file请参阅在线演示
即使您使用的是ERE POSIX正则表达式(使用-r启用),您仍然无法使用类似\d数字模式的PCRE。
此外,我添加了一个捕获组(version=) ,以便能够使用\1反向引用在替换部分中恢复它。
You can use
nbr=13 sed -re "s/(version=)[0-9]+/\1$nbr/g" config.fileSee the online demo
Even if you are using an ERE POSIX regex (enabled with -r), you still cannot use PCRE like \d digit pattern.
Also, I added a capturing group around (version=) to be able to restore it in the replacement part with a \1 backreference.
更多推荐
发布评论