Skip to main content
25 events
when toggle format what by license comment
Jun 25, 2021 at 21:48 comment added Kusalananda @Isaac I don't understand quite what you're on about, but it feels as if we agree that the command that Jasen posted in their comment is not quite what I want to use. You will also find that $$ is indeed the same as $ in an extended regular expression, i.e. anchoring to the end of the line. In a BRE, it matches an $ character at the end of a line, but only if the final $ of the expression is the last character of the expression. These things are trivially tested (but I'd be very interested in an example that shows otherwise).
Jun 25, 2021 at 19:49 comment added user232326 (Cont.) That is not equal to (a|b)c in that matching b that character is consumed by the regex engine. But here, yes, as an special case, the $ is still present. But all that is allowed to match at that point are empty strings as no character is left between the last character and the $. If you want to write that in a loose naive way, it will be: $ followed by none, one, or several empty strings followed by $ again. So, also, no, $$ is not equivalent to $.
Jun 25, 2021 at 19:45 comment added user232326 But also, no, The regex was a bit more complex that what you wrote. It was like ((X*(Y|$)){3})$ or ((something*(other|$)){3})$. When something* match that something (once or several times) or (the only other alternative) the empty string the regex proceeds to match other or end of line. If what was matched was the end of line ($) the regex stays at that position beetween the last character matched and the end of line` without consuming the end of line marker. (Cont.)
Jun 25, 2021 at 19:19 comment added user232326 So, no, The regex that Jasen sowed is not a BRE, as per definition a BRE has no alternation. It is an extension of POSIX BREs which also have alternation. But going from that idea to stating that there is an alternative BRE syntax for an alternation is impossible. There is no way to rewrite that ((;|$)$) without using alternation. @Kusalananda
Jun 25, 2021 at 19:01 comment added user232326 Please read A good explanation of alternation in BRE
Jun 25, 2021 at 18:55 comment added user232326 I understand that from your perception you either have to use extended regexes (which do not have back references, and you need back references to use the \1 in the replacement part os the sed s/// command) or basic regexes. That is the way that your choice of sed works. But thinking that an alternation is equivalent to an optional match (a?) is fundamentally wrong and leads to other mistakes. @Kusalananda
Jun 25, 2021 at 18:46 comment added user232326 There isn't such thing as an equivalent to an alternation in BRE. Obsolete (``basic'') regular expressions differ in several respects. '|' is an ordinary character and there is no equivalent for its functionality. From Freebsd re_format manual. Directly from the horse mouth. @Kusalananda
Jun 25, 2021 at 18:18 comment added Kusalananda @Isaac You could argue that the expression that Jasen showed was a basic regular expression and that $$ therefore matches a $ at the end of a line, but then again, alternations are not available in basic regular expressions so any discussion about that would probably assume that one is using GNU sed, which I'm not doing. Or, you could rewrite their \(;\|$\) as \([;$]\), but then it's just nonsense and no longer related to the data in the question at all.
Jun 25, 2021 at 17:51 comment added Kusalananda @Isaac Not really when the right hand side of the alternation is $. What they showed was something to the effect of (;|$)$. How would you rewrite that without using an alternation? I would rewrite it as ;?$. It's absolutely true that (a|b) is not the same as a?b?, but what was shown was not (a|b), was it? It was (a|b)b, i.e either ab or bb. Since b was $, you get either a$ or $ ($$ is the same as $ in an extended regular expression), i.e. a?$.
Jun 25, 2021 at 15:16 comment added user232326 But of course that (a|b) isn't the same as (a?b?). The former must match either an a or an b, the later could just be empty and match nothing. Same mistake again. @Kusalananda
Jun 25, 2021 at 13:58 comment added Kusalananda @Jasen Not unavailable. I just don't really use GNU sed.
Jun 25, 2021 at 13:22 comment added Jasen yes, I am ans wasn't aware that that was unavailable for you.
Jun 25, 2021 at 13:09 comment added Kusalananda @Jasen You seem to be using GNU sed, right? That \| is an alternation, I believe, which sed ordinarily does not support in basic regular expressions. Isn't (;|$)$ the same as ;?$? I don't really use GNU sed, but if you find that this command works better, then by all means use that instead.
Jun 25, 2021 at 13:07 comment added Jasen actually I was thinking of this sed 's/;\(\([^;]*\(;\|$\)\)\{3\}\)$/,\1/'
Jun 25, 2021 at 9:49 comment added Kusalananda @guest_7 Thanks for your contribution, but I would not answer with a GNU sed solution generally as I don't personally use GNU sed. The code also lacked explanation. You are free and most welcome to add that solution in your own answer though (which is already a GNU sed solution).
Jun 25, 2021 at 9:48 history rollback Kusalananda
Rollback to Revision 6
Jun 25, 2021 at 9:43 history edited guest_7 CC BY-SA 4.0
added 169 characters in body
Jun 24, 2021 at 20:09 history edited Kusalananda CC BY-SA 4.0
added 9 characters in body
Jun 24, 2021 at 20:07 comment added Kusalananda @Isaac Yes it does, which is why I was very careful to add "as long as there are actually that many fields". The second part of my answer does not behave in that way though, you'd be happy to hear, which is why I did not include that wording there.
Jun 24, 2021 at 20:06 comment added user232326 Note that this answer still replaces the ; if less than n ; exist. Try echo "abc;def" | sed 's/;\(\([^;]*;\{0,1\}\)\{3\}\)$/,\1/' which outputs abc,def.
Jun 24, 2021 at 19:26 history edited Kusalananda CC BY-SA 4.0
added 84 characters in body
Jun 24, 2021 at 19:20 history edited Kusalananda CC BY-SA 4.0
added 430 characters in body
Jun 24, 2021 at 18:59 history edited Kusalananda CC BY-SA 4.0
added 694 characters in body
Jun 24, 2021 at 17:18 history edited Kusalananda CC BY-SA 4.0
added 48 characters in body
Jun 24, 2021 at 9:39 history answered Kusalananda CC BY-SA 4.0