10

What does the # character mean in this line:

/bin/sed -e 's#abc#zzz#g'
14

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'
24

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
8
  • It might be worth noting that at least in GNU sed, / 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
  • @steeldriver indeed! - I saw that Ravexina added that to his answer, so I shall perhaps leave mine :) – Zanna Jun 17 '17 at 20:40
  • 1
    It isn't important whether the stream contains the separator character. What is important is whether the search string or replacement string contain the separator character. – kasperd Jun 18 '17 at 8:20
  • @kasperd oh yes, true. I edited slightly :) – Zanna Jun 18 '17 at 12:55
  • @Zanna's answer is more to the point: The first character after the command "s" determines the separator which is used in this command. You could use any character here. sbabeb does the same as s/a/e/. – rexkogitans Jun 19 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.