How to remove ^[, and all of the escape sequences

2019-01-05 02:49发布

We want to remove ^[, and all of the escape sequences.

sed is not working and is giving us this error:

$ sed 's/^[//g' oldfile > newfile; mv newfile oldfile;
sed: -e expression #1, char 7: unterminated `s' command

$ sed -i '' -e 's/^[//g' somefile
sed: -e expression #1, char 7: unterminated `s' command

9条回答
时光不老,我们不散
2楼-- · 2019-01-05 03:37

I built vtclean for this. It strips escape sequences using these regular expressions in order (explained in regex.txt):

// handles long-form RGB codes
^\033](\d+);([^\033]+)\033\\

// excludes non-movement/color codes
^\033(\[[^a-zA-Z0-9@\?]+|[\(\)]).

// parses movement and color codes
^\033([\[\]]([\d\?]+)?(;[\d\?]+)*)?(.)`)

It additionally does basic line-edit emulation, so backspace and other movement characters (like left arrow key) are parsed.

查看更多
Root(大扎)
3楼-- · 2019-01-05 03:42

ansi2txt command (part of kbtin package) seems to be doing the job perfectly on Ubuntu.

查看更多
祖国的老花朵
4楼-- · 2019-01-05 03:44

I managed with the following for my purposes, but this doesn't include all possible ANSI escapes:

sed -r s/\x1b\[[0-9;]*m?//g

This removes m commands, but for all escapes (as commented by @lethalman) use:

sed -r s/\x1b\[[^@-~]*[@-~]//g

Also see "Python regex to match VT100 escape sequences".

There is also a table of common escape sequences.

查看更多
登录 后发表回答