Last Updated: February 25, 2016
·
49.48K
· mrunelov

Delimiters in sed substitution

When using sed's substitute command, this is the most common syntax:

echo foo | sed s/foo/bar/ 

Here sed is used to replace the text foo with bar before it is echoed. The delimiter of choice is a forward slash (/), which is the most commonly used delimiter. However, sed can use any character as a delimiter. In fact, it will automatically use the character following the s as a delimiter.

For example, this works:

echo foo | sed s,foo,bar,

echo foo | sed 's foo bar '

Note that the expression has to be terminated with the delimiter. And in order to use spaces as delimiters the expression needs to be enclosed within quotes.

This is useful when you're, for example, using a lot of forward slashes in your expression, which might make the expression unreadable if you're using forward slashes as delimiters as well.

Here are some sources on the subject:

Using different delimiters in sed

Unix Sed Tutorial: Advanced Sed Substitution Examples