home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 33.6 Change Many Files by Editing Just One Chapter 33
Batch Editing
Next: 33.8 Batch Editing Gotcha: Editors Bomb on Big Files
 

33.7 ed/ex Batch Edits: Avoid Errors When No Match

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  ]

- CT in comp.unix.questions on Usenet, 16 May 1989


Previous: 33.6 Change Many Files by Editing Just One UNIX Power Tools Next: 33.8 Batch Editing Gotcha: Editors Bomb on Big Files
33.6 Change Many Files by Editing Just One Book Index 33.8 Batch Editing Gotcha: Editors Bomb on Big Files

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System










??????????????@Mail.ru