* convert sed to zsh @ 2015-11-26 17:49 Ray Andrews 2015-11-26 20:17 ` Bart Schaefer 0 siblings, 1 reply; 3+ messages in thread From: Ray Andrews @ 2015-11-26 17:49 UTC (permalink / raw) To: Zsh Users I'm trying to convert a 'sed' filter to native zsh. Here's the sed: bar=$( echo $foo | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" ) Here's my candidate zsh: bar=$foo//$'\x1B'\[([0-9](#c0,2)(;[0-9](#c0,2))#)#[mGK]/} ... so far it's working, but I'm nervous about it. In another context it seems I have to backslash the semi-colon, but here it seems ok either way, but of course it should be one or the other. The 'sed' is of course the kosher way (I believe) of removing color codes and various other escapes like "\e[K" that seem to hang around colorized output of 'grep' and so on. With several different forms of color codes, I want to be sure I get this right. Unless of course zsh already has a stock way of stripping escape sequences, which it very well might. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: convert sed to zsh 2015-11-26 17:49 convert sed to zsh Ray Andrews @ 2015-11-26 20:17 ` Bart Schaefer 2015-11-26 21:18 ` Ray Andrews 0 siblings, 1 reply; 3+ messages in thread From: Bart Schaefer @ 2015-11-26 20:17 UTC (permalink / raw) To: Zsh Users On Nov 26, 9:49am, Ray Andrews wrote: } Subject: convert sed to zsh } } sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" } } Here's my candidate zsh: } } bar=$foo//$'\x1B'\[([0-9](#c0,2)(;[0-9](#c0,2))#)#[mGK]/} That's a parse error, you're missing the opening curly brace. You can write $'\x1B' as $'\e' for clarity. Why change {1,2} in sed to (#c0,2) in zsh? (#c1,2) should work. } ... so far it's working, but I'm nervous about it. In another context } it seems I have to backslash the semi-colon, but here it seems ok either } way, but of course it should be one or the other. "Of course"? The reason both ';' and '\;' work there has to do with the properties of patterns rather than the properties of semicolons. Semicolon is a command terminator but otherwise is not special, so you need to protect a literal semicolon only from command parsing [such as inside $(...)]. } The 'sed' is of course the kosher way (I believe) of removing color } codes and various other escapes like "\e[K" that seem to hang around } colorized output of 'grep' and so on. I'm not quite an expert on color codes but the zsh expression should do the same as the sed (modulo 1,2 vs. 0,2 as noted and assuming you have extendedglob set). I do know that there are other escapes (cursor movements, overstriking, etc.) that won't match this expression, but probably you never encounter those in the usage you expect. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: convert sed to zsh 2015-11-26 20:17 ` Bart Schaefer @ 2015-11-26 21:18 ` Ray Andrews 0 siblings, 0 replies; 3+ messages in thread From: Ray Andrews @ 2015-11-26 21:18 UTC (permalink / raw) To: zsh-users On 11/26/2015 12:17 PM, Bart Schaefer wrote: > On Nov 26, 9:49am, Ray Andrews wrote: > } Subject: convert sed to zsh > } > } sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > } > } Here's my candidate zsh: > } > } bar=$foo//$'\x1B'\[([0-9](#c0,2)(;[0-9](#c0,2))#)#[mGK]/} > > That's a parse error, you're missing the opening curly brace. Right. An edit-in-place error. Gotta be more careful about those they waste people's time. > > You can write $'\x1B' as $'\e' for clarity. > > Why change {1,2} in sed to (#c0,2) in zsh? (#c1,2) should work. I'm working from expressions taken verbatim and on faith, but that seem to either very slightly 'disagree' with each other, or to have different sets of codes they'll work with, or one is sightly sloppier than the other, so I'm being careful. There could be syntax in one where no match is acceptable, but an error in the other or some such. I know for most things zsh just uses different symbols vis a vis regex, but there's logic changes too so gotta be alert. > > } ... so far it's working, but I'm nervous about it. In another context > } it seems I have to backslash the semi-colon, but here it seems ok either > } way, but of course it should be one or the other. > > "Of course"? I don't want to guess. If I backslash it will be because it's required. > > The reason both ';' and '\;' work there has to do with the properties > of patterns rather than the properties of semicolons. Semicolon is a > command terminator but otherwise is not special, so you need to protect > a literal semicolon only from command parsing [such as inside $(...)]. Ah, so the backslash is acceptable but not required in the way that you can backslash near anything. > > } The 'sed' is of course the kosher way (I believe) of removing color > } codes and various other escapes like "\e[K" that seem to hang around > } colorized output of 'grep' and so on. > > I'm not quite an expert on color codes but the zsh expression should > do the same as the sed (modulo 1,2 vs. 0,2 as noted and assuming you > have extendedglob set). I do know that there are other escapes (cursor > movements, overstriking, etc.) that won't match this expression, but > probably you never encounter those in the usage you expect. Yeah, just colors, for now, anyway. I'm working from Sebastian's stuff, which started out being able to filter colors, but not the ' \e[K' thing that grep adds. So I added a separate filter for that, but the 'sed' expression seems robust enough to handle that along with the color codes. So I'm reverse engineering that back to zsh syntax. The sed expression has an obvious robustness about it--you can see that it will handle any color code that I know of, anyway. Your edits in place and all good, thanks. > ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-11-26 21:48 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2015-11-26 17:49 convert sed to zsh Ray Andrews 2015-11-26 20:17 ` Bart Schaefer 2015-11-26 21:18 ` Ray Andrews
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).