2

Whenever I use the script command to record the keys I type or when I activate the logging mode in screen, all BS (Backspace) and ESC (Escape) key presses are also included in the file. e.g: cd ~/foo/tpBSBStmp.

Is there a way to auto remove the BS or the ESC from the file so the final recorded command included in the file would be cd ~/foo/tmp?

Btw, this also happens in the logging feature of Putty.

I'm open to any scripts that remove the unwanted characters or even another commands that do the same job

3

The output of script will always contain linefeeds, backspaces and ANSI escape sequences as stated in the manpage. Examples of programs that will correctly display all these are cat and more. cat typescript and more typescript will display typescripts exactly as it looked when you recorded them.

If you still want to clean the typescripts, take a look at this script. It is a Perl script I stumbled upon some time ago that is made specifically for cleaning up typescripts made with script.

Try it by running script-declutter myTypescript > cleanTypescript

Edit: Not really related to the answer, but you might be interested in TermRecord. It creates neat, self contained HTML and Javascript representation of your terminal sessions so that anyone can view them without any knowledge of how to handle typescripts. All they need is a web browser.

  • Thanks a lot. I really appreciate the extra info about TermRecord. – Bingo Jun 2 '14 at 3:17
1

If you use the -vte argument to cat, it will escape the terminal codes.

e.g.

cat -vte myfile
0

The link to script-declutter is not working, but it is shown on a webpage:

#!/usr/bin/perl -wp

# clean up control characters and other non-text detritus that shows up
# when you run the "script" command.

BEGIN {
# xterm titlebar escape sequence
$xtermesc = "\x1b\x5d\x30\x3b";

# the occurence of a backspace event (e.g. cntrl H, cntrol W, or cntrl U)
$backspaceevent = "\x1b\\\x5b\x4b"; # note escaping of third character

# ANSI color escape sequence
$ansiesc = qr/\x1b\[[\d;]*?m/;

# technically, this is arrow-right. For some reason, being used against
# very long backspace jobs. I don't fully understand this, as evidenced
# by the fact that is off by one sometimes.
$bizarrebs = qr/\x1b\[C/;

# used as part of the xterm titlebar mechanism, or when
# a bell sounds, which might happen when you backspace too much.
$bell = "\x07"; # could use \a

$cr = "\x0d"; # could use \r

$backspace = "\x08"; # could use \b
}

s/$xtermesc.+?$bell//g;
s/[$cr$bell]//g;
s/${backspaceevent}//g;
s/$ansiesc//g;
while (s/(.)(?=$backspace)//) { s/$backspace//; } # frickin' sweet
# For every ^H delete the character immediately left of it, then delete the ^H.
# Perl's RE's aren't R, so I wonder if I could do this in one expression.
while (s/(..)(?=$bizarrebs)//) { s/$bizarrebs//; }

# notes
# ^[[7P has been spotted. Based on http://www.google.com/codesearch/p?hl=en#4qbG402gtc0/myScreen.C&q="[7P" it appears to be a numbered cursor jump, moving 7 characters (not sure if left or right).

It is a little more complicated than some other examples, though a few comments are in order:

  • it does account for the use of cursor-left escape sequences used in bash's line-editing (partly: the author seemed to assume that it is used the same way as backspace rather than its actual use for moving the cursor within text that is not altered).
  • it does not account for embedded carriage returns as used by yum for its progress meter.
  • its notion of escape sequences to strip out is limited to the SGR escapes (ending with m), and ignores the modes which end with h or l.

For a (shorter) script which does the last two (but still not cursor-left), see my answer to an older question

how to properly log the output of a console program that frequently updates “parts” of the screen, resulting in a messy log file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.