bash sed 通配符搜索替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5843870/
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
sed wildcard search replace
提问by Josh Bond
I'm using sed on Centos, bash.
我在 Centos、bash 上使用 sed。
I want to replace everything between \plain and }} with a space in the below line of text:
我想用下面一行文本中的空格替换 \plain 和 }} 之间的所有内容:
stuff here \plain \f2\fs20\cf2 4:21-23}} more stuff over here, could be anything.
The text between \plain and }} will vary (different numbers/numbers). How can I do a wildcard to include everything between \plain and }}.
\plain 和 }} 之间的文本会有所不同(不同的数字/数字)。我如何做一个通配符来包含 \plain 和 }} 之间的所有内容。
I was hoping a simple * would grab everything between the two but the wildcard in shell doesn't seem to work like this:
我希望一个简单的 * 可以抓取两者之间的所有内容,但 shell 中的通配符似乎不是这样工作的:
s/\plain *}}/ /g;
The answer may be something incorporating this? [a-zA-Z0-9.] but that doesn't account for the backslashes, colons, and dashes in the text.
答案可能是包含这个的东西?[a-zA-Z0-9.] 但这不考虑文本中的反斜杠、冒号和破折号。
回答by sahaj
Just add dot before * to match everything.
只需在 * 前添加点即可匹配所有内容。
i.e. s/\\plain .*}}/ /gshould work.
即s/\\plain .*}}/ /g应该工作。
回答by slezica
The following regex ...
以下正则表达式...
^\plain .*}}$
... will match lines beginning with \plain, having anything in the middle, and ending with }}.
... 将匹配以 开头\plain、中间有任何内容并以}}.结尾的行。
If that's no use, instead of .*to match everything, use ^(negation) to match everything that's not }}.
如果那没有用,与其匹配所有内容,不如.*使用^(negation) 匹配所有不是}}.

