
Using different delimiters in sed (2010) - navigaid
https://backreference.org/2010/02/20/using-different-delimiters-in-sed/
======
teddyh
The common alternate character, for those cases where normal slashes are not
suitable, is a comma character; ‘,’. With Sed code, readability is always an
issue, and it can be improved by not using just any character and limiting
yourself to as few alternative modes of syntax as possible.

With a bit of exposure, commands like

s/foo/bar/

and

s,foo,bar,

can be read with equal ease.

~~~
cyphar
> The common alternate character, for those cases where normal slashes are not
> suitable, is a comma character; ‘,’.

I've always used "|" since it's so visually similar, and you always want to
quote regular expressions anyway.

~~~
Someone
Doesn’t that confuse you because it is the alternation symbol
([https://regular-expressions.mobi/alternation.html](https://regular-
expressions.mobi/alternation.html)) in many, if not all, regular expression
syntaxes?

~~~
marcosdumay
And comma appears in repetition limits (like in /a{1,2}/). I don't see much
difference.

~~~
bonzini
Repetition in sed would be written \\{1,2\\}, it's unwieldy enough that it's
rarely used.

------
devy
On a related note, I found Bruce Barnett's sed tutorial is one of the best Sed
guide out there:
[http://www.grymoire.com/Unix/Sed.html](http://www.grymoire.com/Unix/Sed.html)

~~~
mdaniel
It was really good, thank you! I learned a ton of new tricks from it

Separately, I feel like I could have a full time job submitting PRs to fix the
GNU-ism of using `sed -i"" ...` versus the BSD/macOS syntax of `sed -i ""
...`. I don't know which of those two camps broke from the other, but holy
hell it _broke_ for sure.

~~~
thisacctforreal
How does the GNU-ism work at a shell level?

In your first example $0 is "sed", $1 is "-i", $2 is "...", no? What
signifigance does the -i"" have in bash apart from 'concat -i to ""'?

The BSD syntax is plain, $1 is the option "-i", $2 is the value for that
option: "", $3 is "...".

Edit: off by one

~~~
mmt
Indeed, appending an empty string to -i doesn't modify it in any way, AFAIK.

It's that the GNU version doesn't require an argument, but, if an argument is
to be provided, it must be done as part of the flag, not as a separate element
of argv. The MacOS version allows either way of providing an argument, so
-i.orig tends to be portable (assuming the -i flag is supported in the first
place).

Differences in how "traditional" (be they sysv or bsd) versus gnu utilities
handle flags and arguments [1] is very well rooted in history, and is hardly
unique to sed.

I suspect the main reason this has been forgotten is that Linux, which ships
with gnu utilities, has been so dominant for so long, though, even before
that, it was difficult to find a then-current unix on which gnu utilities
couldn't be installed.

[1] As the sibling comment points out, the source of difference is getopt, of
which there were more than just two versions.. including not even using a
library

------
SubiculumCode
For one offs, I like to change up my delimiter so that I a) remind myslef I
can and b)think the slashes are ugly dont like escaping. I prefer pipes.

------
giobox
I’ve seen so many hilariously bad workarounds for this in old shell scripts
from lazy engineers who apparently can’t read a man page and don’t realise the
delimiter character is substitutable with more or less any character you like.

This is one of this extremely useful features that a surprisingly large number
of users don’t seem to know about.

~~~
michaelcampbell
Indeed. `/` covers 99% of my cases, `|` covers 99% of the remaining. I think i
might have had to use something "exotic" once or twice in 30+ years now.

------
keyle
Well, I have some bash scripts to improve... great post, another useful fact
on HN.

------
finnh
tilda has always been my go-to; in fact i tend to use it in preference to /
even if there is no literal / in my statement.

s~foo~bar~

I still use / when pointing out typos in PRs, tho :)

s/calulate/calculate/

------
chx
The address one is really useful I wish I knew it this very week. Thanks!

