[comp.unix.questions] Identifier replacesments

mike@cimcor.mn.org (Michael Grenier) (12/16/88)

Does anyone know a simple shell script of program that will
do identifier replacements in a file. I want to rename variables,
etc. I could use /lib/cpp but it tends to remove comments and
introduce other problems. Programs like sed will match
an identifier but it is difficult to replace them without also replacing
those longer words which might contain the identifier in question.

Any ideas?

     -Mike Grenier
     mike@cimcor.mn.org
     uunet!rosevax!cimcor!mike
      bungia!cimcor!mike

leo@philmds.UUCP (Leo de Wit) (12/19/88)

In article <620@cimcor.mn.org> mike@cimcor.mn.org (Michael Grenier) writes:
|
|Does anyone know a simple shell script of program that will
|do identifier replacements in a file. I want to rename variables,
|etc. I could use /lib/cpp but it tends to remove comments and
|introduce other problems. Programs like sed will match
|an identifier but it is difficult to replace them without also replacing
|those longer words which might contain the identifier in question.

The following shell script will convert occurences of a variable name
with an other variable name, using sed. Output goes to stdout. You may
wish to deal with additional features: multiple variable names,
automatic replacement of the old file.

--------- c u t   h e r e --------
#! /bin/sh

case $# in
0|1) echo "Usage: $0 <from_name> <to_name> [files ...]" 1>&2; exit 1;;
*) from=$1; to=$2; shift; shift;;
esac

case $from in
$to) echo "$0: <from_name> and <to_name> must differ" 1>&2; exit 1;;
esac

not="\([^A-Za-z_0-9]\)"                # A 'not identifier character' pattern

exec sed "
s/^$from$/$to/
s/^$from$not/$to\1/
s/$not$from$/\1$to/
/$not$from$not/{
: again
	s/$not$from$not/\1$to\2/g
	t again
}" $*
--------- a n d   h e r e --------

The sed statements handle 4 separate cases, depending on whether there
are any other characters on the same line: nothing before and after the
var, something after the var, something before the var, and something
before and after the var.
In the last case it is important to repeat the substitution while
substitution succeeds, since otherwise constructs like:

      xxxxxxxxx var var xxxxxx

would not have 'var' correctly replaced (since the space inbetween the
'var's cannot be shared as delimiter).
Note the use of a shell variable to place a regular expression in; this
makes the sed stuff somewhat more readable.
Hope this helps -
                  Leo.