What does the #
character mean in this line:
/bin/sed -e 's#abc#zzz#g'
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityIt'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'
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
/
can be replaced by an alternate character in contexts other than the s
(substitute) command - but in that case it needs to be escaped the first time e.g. sed '/foo/d'
(delete lines matching foo
) becomes sed '\#foo#d'
– steeldriver
Jun 17 '17 at 20:39
sbabeb
does the same as s/a/e/
.
– rexkogitans
Jun 19 '17 at 9:44