ed problem

Paul English x3168 pme at io.UUCP
Thu Aug 31 23:57:06 AEST 1989


dinah at krebs.bcm.tmc.edu writes:

>I am having problem with the following script. I want to edit some
>c files that begin with AA_. I and to change all combinations of AA_
>to the corresponding BB_. [...]

>#! /bin/sh
>for files in `grep -i AA_ *.c | awk -F: '{ print $1 }' | sort -u`
>	do
>	ed $files << EOF
>/aa_/
>s/aa_/bb_/p
>/AA_/
>s/AA_/BB_/p
>/Aa_/
>s/Aa_/Bb_/p
>w $files
>q
>EOF

It is not necessary to use grep first to filter out files which
contain the pattern, and in fact it makes extra work. Let ed look for
the pattern. If it doesn't find it, that is ok.

I would recommend something like the following, a general purpose
``edit files and globally replace pattern'' shell script. (I call this
script `edg'.)

    :

    if [ $# -lt 3 ]; then
	echo "usage: $0 OLDSTR NEWSTR FILES"
	exit 1
    fi

    old=$1; new=$2; shift 2

    for file
    do
	echo $file
	ed - $file << +
	    1,\$s'$old'$new'g
	    w
    +
    done

Thus, you would invoke it like this:
    % edg AA_ BB_ *.c
When it edits a file without AA_, ed will complain with a `?', but you
can ignore it.    
-- 
Paul M. English
  Interleaf, Cambridge: pme at ileaf.com, !{sun!sunne,mit-eddie}!ileaf!pme
  UMass/Boston: pme at umb.edu, !harvard!umb!pme



More information about the Comp.unix.questions mailing list