Can I programmatically “burn in” ANSI control codes to a file using unix utils?

前端 未结 2 1455
广开言路
广开言路 2020-11-28 15:05

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

相关标签:
2条回答
  • 2020-11-28 15:19

    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'
    
    0 讨论(0)
  • 2020-11-28 15:27

    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

    escape _ text _
    

    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):

    s/^[[[][<=>?]\{0,1\}[;0-9]*[@-~]//g
    s/^[[]][^^[]*^G//g
    s/^[[]][^^[]*^[\\//g
    :loop
    s/[^^H]^H\(.\)/\1/g
    t loop
    s/^M^M*$//g
    s/^.*^M//g
    s/^[[^[]//g
    

    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.

    0 讨论(0)
提交回复
热议问题