Why does sed not replace all occurrences?

前端 未结 2 1562
鱼传尺愫
鱼传尺愫 2020-11-29 01:06

If I run this code in bash:

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

it gives output:

log dog dos

How can

相关标签:
2条回答
  • 2020-11-29 01:22

    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'
    

    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

    The official documentation for GNU sed is available at http://www.gnu.org/software/sed/manual/

    0 讨论(0)
  • 2020-11-29 01:42

    You have to put a g at the end, it stands for "global":

    echo dog dog dos | sed -r 's:dog:log:g'
                                         ^
    
    0 讨论(0)
提交回复
热议问题