繁体   English   中英

sed:仅用该行中第一个出现的字符串替换单词,直到最后一次匹配

[英]Sed : replace words only with first occurrence of string in the line not till last match

我被困在脚本的某一处。 你能帮忙吗?

问题:有一个带有行的文件:

"<abc>-group ab:cd:ef +define_1 +DEFINE_2 </abc>"

现在,我想从该文件的所有行中删除-group ab:cd:ef 我尝试过

%s/-group .* //g

但是它将删除所有测试直到最后一个空格。 和新的文本将成为<abc></abc>在我想要这样的地方:

<abc>+define_1 +DEFINE_2 </abc>

谢谢。

使用sed

sed 's/-group [[:graph:]]* //' file

在vi / vim中:

%s/-group [[:graph:]]* //g

使用awk

awk '{sub(/-group [^ ]* /,"")}1' file
"<abc>+define_1 +DEFINE_2 </abc>"

还是这个sed

sed -r 's/-group [^ ]* //' file
"<abc>+define_1 +DEFINE_2 </abc>"

您应该将search & replace表达式更改为以下形式:

`%s/-group ab:cd:ef //g`

这样可以避免整行匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM