regex - How do you ignore but preserve ANSI escape codes with sed? -
if print string "hello, \e[38;5;200mworld\e[0m
", might see "hello, world
" word "world
" highlighted strong magenta color (ff00df) assuming terminal supports it.
however, if pipe output of program tree
column
, you'll see alignment broken, since escape codes incorrectly counted in line length calculations.
as more direct example, tried split string characters in sed using following:
sed 's/\(.\)/\1 /g'
...which yields...
h e l l o , 3 8 ; 5 ; 2 0 0 m w o r l d 0 m
... when piped string shown earlier.
how go matching escape codes purposes of counting, replacing, preserving, etc. while editing stream?
that not exactly, looking for, can develop idea further:
you can remove control characters using col
:
$ col -bx
or using sed
:
$ sed -r "s/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|k]//g"
you can use regular expression extract sequences should not counted or processed other way.
another interesting project, can useful ansifilter.
Comments
Post a Comment