cat, pipes, and filters

Morris M. Keesan mmk at d62iwa.mitre.org
Tue Jun 4 03:03:46 AEST 1991


In article <1991May31.165446.1530 at progress.com> root at progress.COM (Root of all Evil) writes:

[various ways to edit a file in place using sed as part of a pipeline, e.g.]
> cat $FILE | sed s/"$ENTRY"/"$NEWENTRY"/ > $FILE

It's interesting that most of the responses have pointed out the problem
(creating the new $FILE before reading the old one), but all have accepted your
initial premise, which is that "sed" is the appropriate tool for editing a file
in place.  It seems to me that the much more obvious approach is to use "ed",
which is designed for editing files, as "sed" isn't.
My approach, tested under Ultrix 4.1:

#!/bin/sh
#this replaces all occurrences of $ENTRY with $NEWENTRY
ed $FILE <<KICKME
g/$ENTRY/s//$NEWENTRY/g
w
q
KICKME



More information about the Comp.unix.shell mailing list