If I run this code in 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?
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/
sed -E 's,foo,bar,g'
doesn't do the global thing. If you change it to sed -E -e 's,foo,bar,g'
it works.
Dec 12, 2018 at 13:51
echo 'dog dog dos\ndog'|sed 's:dog:log:g
You have to put a g
at the end, it stands for "global":
echo dog dog dos | sed -r 's:dog:log:g'
^
man sed
on Ubuntu, the -r
option means "use extended regular expressions". So the given command works fine, although it doesn't need the features of extended regular expressions to work.
Apr 11, 2016 at 1:28
g
flag for global substitution, you don't need the-r
option here either.