To display a file that contains ANSI sequences,
less -r typescript
Or,
less -R typescript
To remove ANSI and backspace sequences from a file, creating a clean newfile
, try:
sed -r ':again; s/[^\x08]\x08\x1b\[K//; t again; s/\x1b_[^\x1b]*\x1b[\]//g; s/\x1B\[[^m]*m//g' typescript >newfile
How it works
-
-r
This turns on extended regular expressions. (On BSD systems,
-r
should be replaced with-E
. Modern versions of GNU sed will accept either-r
or-E
.) -
`:again; s/[^\x08]\x08\x1b[K//; t again
This removes any backspace sequences. These are done one at a time in a loop.
-
s/\x1b_[^\x1b]*\x1b[\]//g
As an
xterm
extension (see documentation),Esc _ something Esc \
will do nothing. This command removes these sequences. -
s/\x1B\[[^m]*m//g
This removes the remaining ANSI sequences which set colors, etc.
This covers all the control sequences that I normally run into. There are a wide variety of extended control sequences and, if your output has some that I haven’t seen, the code may need to be extended.
POSIX or BSD sed
On a BSD or POSIX system, individual commands have to be chained together with -e
options instead of semicolons. Thus, try:
sed -e ':again' -e 's/[^\x08]\x08\x1b\[K//' -e 't again' -e 's/\x1b_[^\x1b]*\x1b[\]//g' -e 's/\x1B\[[^m]*m//g'