4 ms·
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
by teddyh 8y ago
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 8y ago> 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.
- teddyh 8y agoYes, I’ve seen the vertical bar character used for this too. But in my experience the comma character is more commonly used, and my point was that we should all try to use as commonly used syntax as possible. Therefore I encourage everybody to use the comma character as their usual alternative character in order for the world of Sed code to be slightly less confusing.
- ajross 8y agoMy experience, colored more by perl styles than sed per se, is that the vertical bar is the overwhelmingly typical usage, actually. It's what Wall picked for the examples in the camel book, for example.
- laumars 8y agoMy experience has been that there isn’t a de facto standard outside of /. Arguing that people must use some standard you’ve adopted is a little like saying people should use tabs instead of spaces.
- yiyus 8y agoUsing "|" can be confusing when the sed command is part of a pipeline. I agree with the other user that the comma character is more used. It is slightly easier to type too.
- Someone 8y agoDoesn’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?
- michaelcampbell 8y agoIt's rare that I need a sedex with both paths and alternation, but if I do I'll use something other than / or |. That's the point of this article; if what you're using is going to hinder obviousness, USE SOMETHING ELSE. Because... you can.
- marcosdumay 8y agoAnd comma appears in repetition limits (like in /a{1,2}/). I don't see much difference.
- bonzini 8y agoRepetition in sed would be written \{1,2\}, it's unwieldy enough that it's rarely used.
- cyphar 8y agoGiven the whole "sed" vs "sed -E" distinction, I'd say most metacharacters are confusing anyway. Not to mention usually when I'm using "|", I'm going to be replacing paths and don't really use alternation that often.
- falsedan 8y agoI prefer @ because it is so weird and unexpected people are unlikely to misread the command and think it does something with multiple commands tho perl’s qr (quote regex) operator is miles ahead, it even supports natural pairs of opening/closing delimiters
- laumars 8y agoI really like doing the following in Perl: 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).
- agumonkey 8y agos,,,g is my favorite because it avoid /\//\\/ epilespy
- jolmg 8y agoI tend to use : as the alternate delimiters.