Sed – Match Special Color Characters

regular expressionsedshell

I have a file containing special color encoding characters:

$ cat zz
aaa.gpg
bbb.gpg
ccc.gpg

$ cat -A zz
^[[38;5;216maaa.gpg^[[00m$
^[[38;5;216mbbb.gpg^[[00m$
^[[38;5;216mccc.gpg^[[00m$

I need to use sed command, to match the ending .gpg and remove it. So, if there were no special characters, I would use:

cat zz | sed 's/\.gpg$//'

So how can I match the .gpg^[[00m$ pattern with sed ?

I tried all possible permutations, but still does not work. For example:

cat zz | sed 's/\.gpg\^\[\[00m$//'

Best Answer

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`
...
Related Question