[Chapter 51] 51.6 Cleaning script Files
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:57:22Z

#

| Chapter 51 Miscellaneous Useful Programs and Curiosities |

## 51.6 Cleaning script Files

As article 51.5 explains, the files made by the script program can have stray control characters in them. The shell script called script.tidy can clean them up. Dan Bernstein wrote it and posted it to Usenet; I made a few changes. It reads from files or standard input; it writes to standard output.

script.tidy uses the sed (34.24) substitute command to remove CTRL-m (RETURN) characters from the ends of lines. It uses the sed test command (34.20) to repeat a series of commands that delete a character followed by CTRL-h (BACKSPACE). If you use DELETE as your erase character (5.9), change the script to eat DELETE instead of BACKSPACE. script.tidy uses a trick (45.35) with echo and tr to store the control characters in shell variables. Because the sed script has doublequotes (8.14) around it, the shell variables are substituted in the right places before the shell starts sed.

    eval

    exec
    #!/bin/sh
    # Public domain.

    # Put CTRL-M in $m and CTRL-H in $b.
    # Change \010 to \177 if you use DEL for erasing.
    eval `echo m=M b=H | tr 'MH' '\015\010'`

    exec sed "s/$m\$//
    :x
    s/[^$b]$b//
    t x" $*

You can also hack the sed script in script.tidy to delete some of your terminal's escape sequences (5.8); article 41.11 explains how to find these sequences. (A really automated script.tidy would read your termcap or terminfo entry and look for all those escape sequences in the script file.)

- JP

51.5 Copy What You Do with script |  | 51.7 When You Get Impatient