bash sed 替换字符串中的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14990314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:36:49  来源:igfitidea点击:

sed replacing numbers in string

linuxmacosbashsed

提问by tim peterson

I can replace text in a string but as soon as I try and put in numbers it fails to match.

我可以替换字符串中的文本,但是一旦我尝试输入数字,它就无法匹配。

NOTEthat this [0-9]\+\is causing the problem.

请注意,这[0-9]\+\是导致问题的原因。

This works:

这有效:

sed -i "" -e "s/mySite-.js/mySite$current_timestamp.js/" template.php

This doesn't:

这不会:

sed -i "" -e "s/mySite-[0-9]\+\.js/mySite$current_timestamp.js/" template.php

I apologize for asking this question earlierbut I've spent almost a full day on this stupid thing and I'm going crazy.

我很抱歉之前问过这个问题,但我在这个愚蠢的事情上花了将近一整天的时间,我快疯了。

回答by shellter

Please consider including sample data, and expected output.

请考虑包括示例数据和预期输出。

If you're looking to remove repeating numbers with your [0-9]\+, try removing the \and just use [0-9]+.

如果您想用 删除重复数字,请[0-9]\+尝试删除\并使用[0-9]+.

If that doesn't work then your sed doesn't support +, so use

如果这不起作用,那么你的 sed 不支持+,所以使用

 sed -i "" -e "s/mySite-[0-9][0-9]*\.js/mySite$current_timestamp.js/"  template.php

IHTH

IHTH