Super User Asked on November 27, 2021
Examining the output from
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";'
in a text editor (e.g., vi
) shows the following:
^[[37mABC
^[[0m
How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output through a stream editor of sorts.
The following does not work
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' | perl -pe 's/^[[37m//g' | perl -pe 's/^[[0m//g'
Python port of Adam Katz's excellent and comprehensive perl answer:
def escape_ansi(line):
re1 = re.compile(r'x1b[[x30-x3f]*[x20-x2f]*[x40-x7e]')
re2 = re.compile(r'x1b[PX^_].*?x1b\')
re3 = re.compile(r'x1b][^a]*(?:a|x1b\)')
re4 = re.compile(r'x1b[[]A-Z\^_@]')
# re5: zero-width ASCII characters
# see https://superuser.com/a/1388860
re5 = re.compile(r'[x00-x1fx7f-x9fxad]+')
for r in [re1, re2, re3, re4, re5]:
line = r.sub('', line)
return line
This includes the C0/C1 sequence removal, so remove that if you don't need it. I realize this is not optimized since it's multiple regex passes, but it did the trick for me and optimization wasn't a concern for me.
Answered by Kevin on November 27, 2021
I've had to look this up too many times, so I decided to make a free online tool for it. No need to remember sed commands for this!
Hope it works well for you, too: https://maxschmitt.me/ansistrip/
Answered by Macks on November 27, 2021
There's also a dedicated tool for the job: ansifilter. Use the default --text
output format.
Answered by Juan on November 27, 2021
Combining @Adam-Katz @Mike answers I get:
sed -E $'s|x1b\[[0-\?]*[ -/]*[@-~]||g;
s|x1b[PX^_][^x1b]*x1b\\||g;
s:x1b\][^x07]*(x07|x1b\\)::g;
s|x1b[@-_]||g'
This should work on macos, linux, and mingw64x (Git for Windows)
Note: On super old GNU sed (pre 4.2), the -E
flag needs to be replaced with -r
(like CentOS 6.0 old)
1st: An ANSI CSI Code consists of (in order)
x1b
[
0x30-0x3f
0x20-0x2f
0x40-0x7f
2nd and 3rd: I'm unfamiliar with with in practice, but have read about them in the linked page.
4th: Just a catch all to get all remaining escape codes, assuming there are zero extra bytes. As these codes could do anything they want, it's possible data bytes get left behind, but extremely unlikely as they aren't used much in practice.
Answered by Andy on November 27, 2021
https://unix.stackexchange.com/a/527259/116915
cat typescript | ansi2txt | col -b
ansi2txt
: remove ANSI color codescol -b
: remove ^H
or ^M
Answered by yurenchen on November 27, 2021
"tput sgr0" left this control character ^(B^[
Here is a modified version to take care of that.
perl -pe 's/e[[(][0-9;]*[mGKFB]//g' logfile.log
Answered by GustafAnkarloo on November 27, 2021
I believe this is an authoritative removal of all ANSI escape sequences:
perl -pe '
s/e[[x30-x3f]*[x20-x2f]*[x40-x7e]//g;
s/e[PX^_].*?e\//g;
s/e][^a]*(?:a|e\)//g;
s/e[[]A-Z\^_@]//g;'
(Please note that perl, like many other languages (but not sed), accepts e
as the escape character Esc, x1b
or