sed Newlines, Quoting, and Backslashes in a Shell Script

Feeding sed () 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 (). -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 () with this input. The C shell, however, thinks it is smarter than you are (), and removes the trailing backslashes (), 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 (), to keep the shell's sticky fingers off them.

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