format output from Unix “script” command: remove backspaces, linefeeds and deleted chars?

假如想象 提交于 2019-12-05 03:32:16

The col command will do some, but not all, of the filtering you're looking for. (It doesn't seem to recognize the control sequences for bold and underlining, for example.)

An approach I've used in the past is to (a) change my shell prompt so it doesn't do any highlighting (it normally does), and/or (b) set $TERM to "dumb" so various commands won't try to use certain control sequences.

hnkchnsk

I solved the problem by running scriptreplay in a screen and the dumping the scrollback buffer to a file.

The following expect script does this for you.

It has been tested for logfiles with up to 250.000 lines. In the working directory you need your scriptlog, a file called "time" with 10.000.000 times the line "1 10" in it, and the script. I needs the name of your scriptfile as command line argument, like ./name_of_script name_of_scriptlog.

#!/usr/bin/expect -f 

set logfile [lindex $argv 0]

if {$logfile == ""} {puts "Usage: ./script_to_readable.exp \$logfile."; exit}

set timestamp [clock format [clock sec] -format %Y-%m-%d,%H:%M:%S]
set pwd [exec pwd]
if {! [file exists ${pwd}/time]} {puts "ERROR: time file not found.\nYou need a file named time with 10.000.000 times the line \"1 10\" in the working directory for this script to work. Please provide it."; exit}
set wc [exec cat ${pwd}/$logfile | wc -l]
set height [ expr "$wc" + "100" ]
system cp $logfile ${logfile}.tmp
system echo $timestamp >> ${logfile}.tmp
set timeout -1
spawn screen -h $height -S $timestamp 
send "scriptreplay -t time -s ${logfile}.tmp 100000 2>/dev/null\r"
expect ${timestamp} 
send "\x01:hardcopy -h readablelog.${timestamp}\r"

send "exit\r"

system sed '/^$/d' readablelog.$timestamp >> readablelog2.$timestamp
system head -n-2 readablelog2.$timestamp >> ${logfile}.readable.$timestamp
system rm -f readablelog.$timestamp readablelog2.$timestamp ${logfile}.tmp

The time file can be generated by

for i in $(seq 1 10000000); do echo "1 10" >> time; done

As mentioned by Keith, col does part of the job (the control characters).

You can further use ansifilter to remove any ANSI escape sequences that you don't want: http://www.andre-simon.de/zip/download.html#ansifilter

Or you can use the "more" command, which will interpret those characters and display exactly what you typed, received as output, etc, as if you scrolled back in your buffer.

# awk script
{
    gsub(/\033\[[CK]/, "")
    while (sub(/.\b/, "")) ;
    print
}

The script removes interleaving 'ESC [ C' and 'ESC [ K' substrings. Then replaces 'c BS' substrings to nothig, where c stands for any character.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!