sed: match special color characters

In order to remove ansi sequences (color and move) we can run something along the lines of

perl -pe 's/\e\[[0-9;]*[mGKHF]//g'

After that, things became much more clear...


c=$(printf '\\(\33\\[[0-9;]*m\\)*')

Would store in $c a regexp that matches any number of graphic attribute setting sequences (colouring, bold, reverse video...), also known as sgr (set graphic rendition).

Then:

sed "s/${c}\.${c}g${c}p${c}g\(${c}\)\$/\5/"

Would remove a trailing .gpg including interspersed and preceding SGR sequences, but preserving trailing ones (like your \e[00m (sgr0) to restore default graphic rendition).


What you see on the terminal as ^[ is the escape character. The second [ is a [.

You need to include the code for escape.

replace the ^[ with an escape character.

esc="$(echo '\033')"
sed 's/\.gpg'"${esc}"'\[00m$//'

or

esc='\x1b`
...