59 points · by navigaid · 7y ago · 29 comments
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.
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
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
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
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
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/