Q: My Bourne shell script (44.1) calls ed to edit a set of files:
Q:
= for << \ | site=something cmty=somethingelse for i in file1 file2 file3 do ed $i << end 1,\$s/pat1/$site/g 1,\$s/pat2/$cmty/g w q end done |
---|
Q: It works fine except when one of the files does not contain pat1. ed doesn't update that file, even though it could have matched pat2. The other files are edited as they should be.
A: On an error - including "no matches" - ed attempts to
discard any unread commands. If you are running ed "by hand"
this has no effect, but if its input is from a file, this makes
EOF -d-of-file) the next thing it sees. You could remove the q
command and you would see the same behavior, as ed automatically quits
at end-of-file.
There is a simple workaround. Unlike the s
command, the
global command g
does not report an error if no lines match.
Thus:
ed - $i << end g/pat1/s//$site/g g/pat2/s//$cmty/g w end
The - (dash) flag suppresses the two numbers
that ed normally prints when reading and writing files.
These are the number of characters in the file,
and are usually irrelevant.
[As Chris explained, the q
in the original script isn't needed.
-JP ]
- in comp.unix.questions on Usenet, 16 May 1989