bash 使用带通配符的 SED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9189120/
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-09 21:38:14  来源:igfitidea点击:

Using SED with wildcard

bashsedwildcard

提问by mahmood

I want to replace a string with wildcard but it doesn't work.

我想用通配符替换字符串,但它不起作用。

The string looks like "some-string-8"

字符串看起来像“some-string-8”

I wrote

我写

sed -i 's/string-*/string-0/g' file.txt

but the output is

但输出是

some-string-08

回答by tom

The asterisk (*) means "zero or more of the previous item".

星号 (*) 表示“前一项的零个或多个”。

If you want to match any single character use

如果要匹配任何单个字符,请使用

sed -i 's/string-./string-0/g' file.txt

If you want to match any string (i.e. any single character zero or more times) use

如果要匹配任何字符串(即任何单个字符零次或多次),请使用

sed -i 's/string-.*/string-0/g' file.txt

回答by Michael Manoochehri

So, the concept of a "wildcard" in Regular Expressions works a bit differently. In order to match "any character" you would use "." The "*" modifier means, match any number of times.

因此,正则表达式中“通配符”的概念有点不同。为了匹配“任何字符”,您可以使用“。” “*”修饰符表示匹配任意次数。