Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!bbn!ileaf!io!pme From: pme@io.UUCP (Paul English x3168) Newsgroups: comp.unix.questions Subject: Re: ed problem Message-ID: <1229@io.UUCP> Date: 31 Aug 89 13:57:06 GMT References: <1685@gazette.bcm.tmc.edu> Distribution: usa Organization: Interleaf Inc, Cambridge, MA Lines: 54 dinah@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@ileaf.com, !{sun!sunne,mit-eddie}!ileaf!pme UMass/Boston: pme@umb.edu, !harvard!umb!pme