Change sed’s default delimiter to delete line(s)

The problem

When you want to delete a line or several in a file using sed you need to use that syntax:

root@machine:~# sed -i '/mymatch/d' test

This will match all lines containing mymatch and delete them from the file (smartly called here test).

But what if the string you want to match contains the character / in it? Let’s try:

root@machine:~# sed -i '/http://example/d' test
sed: -e expression #1, char 8: unknown command: `/'

Obviously it fails, so what about using an other character to delimit? One could try that:

root@machine:~# sed -i '|http://example|d' test
sed: -e expression #1, char 1: unknown command: `|'

It fails too, you can try with the # character, this time it won’t give you an error, but it will just do nothing.

A solution

One solution is very simple, it’s just to backslash the character you want as the new delimiter like that:

root@machine:~# sed -i '|http://example|d' test

P.S: again, it’s pretty obvious but don’t use a character as delimiter which is already in the match, else you are back to the begining of this post. Be careful when your match is a variable.