bash 为什么 sed 不替换所有出现的事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15849119/
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
Why does sed not replace all occurrences?
提问by Nishant George Agrwal
If I run this code in bash:
如果我在 bash 中运行此代码:
echo dog dog dos | sed -r 's:dog:log:'
it gives output:
它给出了输出:
log dog dos
How can I make it replace all occurrences of dog?
我怎样才能让它取代所有出现的狗?
回答by Bruno Reis
You should add the g
modifier so that sed performs a globalsubstitution of the contents of the pattern buffer:
您应该添加g
修饰符,以便 sed 执行模式缓冲区内容的全局替换:
echo dog dog dos | sed -e 's:dog:log:g'
For a fantastic documentation on sed, check http://www.grymtheitroade.com/Unix/Sed.html. This globalflag is explained here: http://www.grymtheitroade.com/Unix/Sed.html#uh-6
有关 sed 的精彩文档,请查看http://www.grymtheitroade.com/Unix/Sed.html。这里解释了这个全局标志:http: //www.grymtheitroade.com/Unix/Sed.html#uh-6
The official documentation for GNU sed
is available at http://www.gnu.org/software/sed/manual/
官方文档GNU sed
可在http://www.gnu.org/software/sed/manual/ 获得
回答by alestanis
You have to put a g
at the end, it stands for "global":
你必须g
在最后放一个,它代表“全局”:
echo dog dog dos | sed -r 's:dog:log:g'
^