bash sed 替换星号

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

Sed replace asterisk symbols

regexbashsed

提问by user1516252

I'm am trying to replace a series of asterix symbols in a text file with a -999.9 using sed. However I can't figure out how to properly escape the wildcard symbol.

我正在尝试使用 sed 用 -999.9 替换文本文件中的一系列星号。但是我不知道如何正确转义通配符。

e.g.

例如

$ echo "2006.0,1.0,************,-5.0" | sed 's/************/-999.9/g'
sed: 1: "s/************/-999.9/g": RE error: repetition-operator operand invalid

Doesn't work. And

不起作用。和

$ echo "2006.0,1.0,************,-5.0" | sed 's/[************]/-999.9/g'
2006.0,1.0,-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9,-5.0

puts a -999.9 for every * which isn't what I intended either.

为每个 * 设置 -999.9,这也不是我想要的。

Thanks!

谢谢!

回答by sat

Use this:

用这个:

echo "2006.0,1.0,************,-5.0" | sed 's/[*]\+/-999.9/g'

Test:

测试:

$ echo "2006.0,1.0,************,-5.0" | sed 's/[*]\+/-999.9/g'
2006.0,1.0,-999.9,-5.0

回答by Ed Morton

Any of these (and more) is a regexp that will modify that line as you want:

这些(以及更多)中的任何一个都是一个正则表达式,可以根据需要修改该行:

$ echo "2006.0,1.0,************,-5.0" | sed 's/\*\**/999.9/g'
2006.0,1.0,999.9,-5.0

$ echo "2006.0,1.0,************,-5.0" | sed 's/\*\+/999.9/g'
2006.0,1.0,999.9,-5.0

$ echo "2006.0,1.0,************,-5.0" | sed -r 's/\*+/999.9/g'
2006.0,1.0,999.9,-5.0

$ echo "2006.0,1.0,************,-5.0" | sed 's/\*\{12\}/999.9/g'
2006.0,1.0,999.9,-5.0

$ echo "2006.0,1.0,************,-5.0" | sed -r 's/\*{12}/999.9/g'
2006.0,1.0,999.9,-5.0

$ echo "2006.0,1.0,************,-5.0" | sed 's/\*\{1,\}/999.9/g'
2006.0,1.0,999.9,-5.0

$ echo "2006.0,1.0,************,-5.0" | sed -r 's/\*{1,}/999.9/g'
2006.0,1.0,999.9,-5.0

sed operates on regular expressions, not strings, so you need to learn regular expression syntax if you're going to use sed and in particular the difference between BREs (which sed uses by default) and EREs (which some seds can be told to use instead) and PCREs (which sed never uses but some other tools and "regexp checkers" do). Only the first solution above is a BRE that will work on all seds on all platforms. Google is your friend.

sed 操作正则表达式,而不是字符串,因此如果您要使用 sed,则需要学习正则表达式语法,特别是 BRE(sed 默认使用)和 ERE(某些 sed 可以被告知使用)之间的区别相反)和 PCRE(sed 从不使用,但其他一些工具和“正则表达式检查器”使用)。只有上面的第一个解决方案是适用于所有平台上的所有 sed 的 BRE。谷歌是你的朋友。

回答by anubhava

*is a regex symbol that needs to be escaped.

*是一个需要转义的正则表达式符号。

You can even use BASH string replacement:

您甚至可以使用 BASH 字符串替换:

s="2006.0,1.0,************,-5.0"
echo "${s/\**,/-999.9,}"
2006.0,1.0,-999.9,-5.0

Using sed:

使用sed

sed 's/\*\+/999.9/g' <<< "$s"
2006.0,1.0,999.9,-5.0

回答by Avinash Raj

Ya, *are special meta character which repeats the previous token zero or more times. Escape *in-order to match literal *characters.

是的,*是特殊的元字符,它重复前面的标记零次或更多次。转义*以匹配文字*字符。

sed 's/\*\*\*\*\*\*\*\*\*\*\*\*/-999.9/g'

回答by Claes Wikner

When this possibility was introduced into gawk I have no idea!

当这种可能性被引入 gawk 时,我不知道!

gawk -F, '{sub(/************/,"-999.9",)}1' OFS=, file
2006.0,1.0,-999.9,-5.0