Example: I start recording with script
, and try to type echo test
but omit the o, so I backspace to correct it.
When I cat typescript
everything looks normal, since the codes are interpreted, but if I use less
or vim
I see ech test^H^[[K^H^[[K^H^[[K^H^[[K^H^[[Ko test^M
I fully understand what this is and why it's happening, but is there any way to "burn in" the codes and just see the result in a file? My kludgy method is to cat
the file, then copy/paste the text out of the terminal, but surely some combination of cat, sed, awk, or something else can get me there more easily?
The suggested answer using "sed -r" relies upon GNU sed, which makes it not really portable. It is possible to do the same functionality with POSIX sed, but differently: POSIX does not provide for passing a whole script in a command option as shown here. That means that the (POSIX) way to implement a loop would be in a separate file, passed to sed using the "-f" option. Likewise, the hexadecimal constants are not portable. After making these changes, a functionally equivalent script can be used on the BSDs and Unix systems.
The suggested answer also does not cover some of the uses of carriage returns which are fairly common (for instance in yum output), nor does it filter out "most" ANSI sequences (since it focuses on the SGR "m" final character). Finally, it refers to
as an xterm extension. But no such extension is provided by xterm, because the two characters "escape" and "_" begin an Application Program Command sequence (and xterm implements none).
The resulting sed-script looks like this ("^[" is the escape character):
A more complete script, named "script2log" can be found here. There are, however, things (such as CSI K) which are not suited to a sed script.
To display a file that contains ANSI sequences,
Or,
To remove ANSI and backspace sequences from a file, creating a clean
newfile
, try: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: