Using different delimiters in sed (2010)
backreference.org
backreference.org
With a bit of exposure, commands like
s/foo/bar/
and
s,foo,bar,
can be read with equal ease.
I've always used "|" since it's so visually similar, and you always want to quote regular expressions anyway.
Arguing that people must use some standard you’ve adopted is a little like saying people should use tabs instead of spaces.
@
because it is so weird and unexpected people are unlikely to misread the command and think it does something with multiple commandstho perl’s qr (quote regex) operator is miles ahead, it even supports natural pairs of opening/closing delimiters
s{/u/r/i}{do something}
Sadly doesn’t support that (or at least none of the sed’s I’ve used). So I fallback to using hash (#) instead (for no particular reason aside that’s what I’ve always used).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.
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
https://git.savannah.gnu.org/cgit/sed.git/tree/sed/sed.c?h=v...
https://github.com/freebsd/freebsd/blob/release/7.0.0/usr.bi...
So, while your interpretation of the shell splitting is correct, it's the different getopt declarations that cause this pain
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
What's the use case [1] for using a zero-length suffix argument (i.e. editing the file "in place" with no backup)?
It's not as if sed is operating on the file actually in place. It uses a temporary file anyway. Varying levels of reliability/portability can be achieved by controlling that temporary file (or subsequent artifacts) oneself, the first level being just to "rm" the backup.
[1] Assuming this is in scripting, since the interactive situation is easily enough adjusted on the fly
I've seen plenty of un-portable shell scripting, too, but my professional experience includes a time where, essentially, no software [1] could be assumed to be portable, and I did get some dollars for every time, since it was part of my job to ensure as consistent a build environment as possible.
In light of your clarification, my question becomes: isn't it actually good to have such assumption-breaking differences in that they call attention to something that is likely to have a broader pattern of non-portability, in which case a broadly effective [2] workaround can be applied?
[1] Even/especially GNU tools, where there was something of an assumption that the OS would provide at least fairly complete BSD-compatibility. The existence, and evolution, of libiberty and the autotools, among others, should be instructive.
[2] e.g. installing (all the) GNU tools and putting them first in the path on a system that otherwise uses "traditional" syntax
This is one of this extremely useful features that a surprisingly large number of users don’t seem to know about.
s~foo~bar~
I still use / when pointing out typos in PRs, tho :)
s/calulate/calculate/