为什么 sed 不替换所有出现的? [英] Why does sed not replace all occurrences?

查看:20
本文介绍了为什么 sed 不替换所有出现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 bash 中运行此代码:

If I run this code in bash:

echo dog dog dos | sed -r 's:dog:log:'

它给出输出:

log dog dos

我怎样才能让它替换所有出现的狗?

How can I make it replace all occurrences of dog?

推荐答案

您应该添加 g 修饰符,以便 sed 执行模式内容的全局替换缓冲区:

You should add the g modifier so that sed performs a global substitution of the contents of the pattern buffer:

echo dog dog dos | sed -e 's:dog:log:g'

有关 sed 的精彩文档,请查看 http://www.grymoire.com/Unix/Sed.html.这个全局标志解释如下:http://www.grymoire.com/Unix/Sed.html#uh-6

For a fantastic documentation on sed, check http://www.grymoire.com/Unix/Sed.html. This global flag is explained here: http://www.grymoire.com/Unix/Sed.html#uh-6

GNU sed 的官方文档可从 http://www.gnu 获得.组织/软件/sed/手册/

这篇关于为什么 sed 不替换所有出现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆