[Chapter 34] 34.23 sed Newlines, Quoting, and Backslashes in a Shell Script
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:47:34Z

#

| Chapter 34 The sed Stream Editor |

## 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

34.22 Dangers of the sed Quit Command |  | 34.24 Quick Reference: sed