home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 51.5 Copy What You Do with script Chapter 51
Miscellaneous Useful Programs and Curiosities
Next: 51.7 When You Get Impatient
 

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


Previous: 51.5 Copy What You Do with script UNIX Power Tools Next: 51.7 When You Get Impatient
51.5 Copy What You Do with script Book Index 51.7 When You Get Impatient

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System