Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: convert sed to zsh



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.



Messages sorted by: Reverse Date, Date, Thread, Author