在命令中查找具有特定开头和结尾的字符串,并用另一个字符串替换。

huangapple go评论57阅读模式
英文:

Find strings in a file with certain beginning and ending and replace with another string using command

问题

我想要将文件中所有表达式模式为abc + something )))的部分替换为cba)。第一个表达式中的"something"在所有情况下都不同。

例如:将abc, ifojg)))更改为cba);或将abc "asdf")))更改为cba)

我尝试过:

sed -i 's/abc*)))/new/g' file

但它对文件没有产生任何影响。我应该如何修改这段代码?我对正则表达式不熟悉。请解释答案中的每个符号。谢谢!

英文:

I want to replace all the expression with pattern abc + something ))) to cba) in a file. The something in the first expression is different in all the instances.
For example: change abc, ifojg))) to cba); or change abc "asdf"))) to cba).
I've tried:

sed -i 's/abc*)))/new/g' file

But it did nothing on the file. How should I modify the code? I am not familiar with the regular expression. Please explain every symbol in the answer. Thanks!

答案1

得分: 0

echo 'abc, ifojg)))' | sed -r 's/abc[^)]+)))/cba)/'

英文:
echo 'abc, ifojg)))' | sed -r 's/abc[^\)]+\)\)\)/cba)/'

The pattern is abc followed by characters other than ) , followed by )))

huangapple
  • 本文由 发表于 2020年1月6日 20:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定