[alt.sources] [comp.unix.shell] file conversion

michaelp@dadla.WR.TEK.COM (Michael Prusynski) (10/19/90)

Archive-name: cnvt/17-Oct-90
Original-posting-by: michaelp@dadla.WR.TEK.COM (Michael Prusynski)
Original-subject: file conversion (was Re: How to strip edit characters)
Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)

[Reposted from comp.unix.shell.
Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]


With the recent discussion on stripping ^H and ^M etc. from files,
I thought I'd contribute my general purpose "cnvt" shell script.
Bill Pfiefer was the originator of this program, but I've modified it
and added to it quite a bit.  The "delbs" function is for nroff output,
and also deletes the underscore or bold character.  b2ul is not guaranteed.
The type of conversion you want must be the first argument.
---------- cut here -----------
: cnvt - Michael Prusynski - July,1987
PATH=/bin:$PATH
usage="usage: cnvt {addcr,delbs,delcr,delff,b2u,cr2lf,lf2cr,indent,sp2tab} file ..."
case $# in
	0|1)
	echo 'convert file contents by one of the following methods:
	addcr - adds a carriage return in front of every line feed
	delbs - deletes backspaces and bold or underline characters
	delcr - deletes carriage returns from file
	delff - deletes form feeds from file
	b2ul  - converts boldface text in nroff output to underlined
	cr2lf - converts carriage returns to line feeds
	lf2cr - converts line feeds to carriage returns
	indent - indents text 5 spaces
	sp2tab - converts spaces to tabs via "unexpand"'
	echo $usage; exit 1;;
esac

function=$1
shift

for x do
    if test ! -f $x; then
	echo "$x: not a file"
    else
	case $function in
		addcr)	sed s/$/
		delcr)	tr -d '\015' <$x >$x.$$;;
		delff)	tr -d '\014' <$x >$x.$$;;
		cr2lf)	tr '\015' '\012' <$x >$x.$$;;
		lf2cr)	tr '\012' '\015' <$x >$x.$$;;
		b2ul)	sed -e '/\.\.\./s//\_/g' $x >$x.$$;;
		delbs)	sed s/_//g $x | sed s/.//g >$x.$$;;
		dblsp)	sed "s:$::" $x | tr "\001" "\012" >$x.$$;;
		indent)	sed '/./s/^/     /' $x >$x.$$;;
		sp2tab)	unexpand -a <$x >$x.$$;;
		*)	echo $usage; exit 1;;
	esac
	test -n $x.$$ && mv $x.$$ $x
    fi
done