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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 34.22 Dangers of the sed Quit Command Chapter 34
The sed Stream Editor
Next: 34.24 Quick Reference: sed
 

34.23 sed Newlines, Quoting, and Backslashes in a Shell Script

Feeding sed (34.24 ) newlines is easy; the real trick is getting them past the C shell.

The sed documentation says that in order to insert newlines in substitute commands, you should quote them with backslashes. [Surround the commands with single quotes (' ), as Chris has. If you use double quotes (" ), this script will become s/foo/bar/ because of the way quoting works with backslashes and newlines (8.14 ) . -JP ]:

sed -e 's/foo/b\
a\
r/'

Indeed, this works quite well in the Bourne shell, which does what I consider the proper thing (8.14 ) with this input. The C shell, however, thinks it is smarter than you are (47.2 ) , and removes the trailing backslashes (8.15 ) , and instead you must type:

sed -e 's/foo/b\\
a\\
r/'

Probably the best solution is to place your sed commands in a separate file (34.2 ) , to keep the shell's sticky fingers off them.

- CT in net.unix on Usenet, 20 November 1985


Previous: 34.22 Dangers of the sed Quit Command UNIX Power Tools Next: 34.24 Quick Reference: sed
34.22 Dangers of the sed Quit Command Book Index 34.24 Quick Reference: sed

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