What does the # character mean in: /bin/sed -e 's#abc#zzz#g'?

Solution 1:

It's a delimiter or separator. The most commonly used one is / as in

sed 's/old/new/' file

But sed will take the first character after the command (s) as delimiter. You can use any convenient character, for example...

sed 's%old%new%' file

This is very useful if the file contains / (or other conventional delimiting characters). You can choose as separator some character that you know you won't need to put into your sed expression, saving you a lot of annoying escaping.

Let's say you want to replace

https://askubuntu.com/questions

with

https://askubuntu.com/posts

You could use

sed 's/https:\/\/askubuntu.com\/questions/https:\/\/askubuntu.com\/posts/' file

But better to use

sed 's|https://askubuntu.com/questions|https://askubuntu.com/posts|' file

Solution 2:

It's a separator, just like "/", it's same as 's/abc/zzz/g'.

it means search for "abc" replace it with "zzz", with global flag, means do it for all "abc"s on the line, not just the first one.

You can also use an alternative separator for a pattern address, but in that case, you need to escape it for it to be correctly interpreted:

sed -r '\#abc#p'