PSA: Sed Delimeters Can be Anything

This is another of my “remember this for later” type of post, but sed has tripped me up several times recently with delimeters, so i’m putting this here.

Recently, while debugging a script I use for putting Varnish configurations together, I ran into the “sawtooth” effect when trying to modify a simple command … or at least, what I thought was a simple command, until running into the first “**” character that I needed to edit.

So a line such as:

\\ configuration sample 0

Meant a sed expression of:

$ sed 's/\/\/configuration\/sample\/0\//\/\/configuration\/sample\/1\//'

Which, aside from looking weird, is also a pain for a non-sed users (or ninja) to use — so having looked at Stack Overflow, commandlinefu and even the bash scripting guide and seeing numerous recommendations to use perl, awk or something that wasn’t actually sed, I decided to refine my searching to “escaping single slash, sed” and found the answer I was looking for.

$ sed 's|\\configuration sample 0|\\ configuration sample 1|'

or:

$ sed 's_\\configuration sample 0_\\configuration sample 1_'

Do exactly the same job, look neater and mean the poor people reading your code don’t have to wonder about the voodoo you’ve added for your sed lines, because it’s clearer too.