Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 34.6 One Thing at a Time Chapter 34
The sed Stream Editor
Next: 34.8 Newlines in a sed Replacement
 

34.7 Delimiting a Regular Expression

Whether in sed or vi, when using the substitution command, a delimiter is required to separate the search pattern from the replacement string. The delimiter can be any character except blank or a newline (vi seems to be more restrictive than sed), although the usual practice is to use the slash (/) as a delimiter (for example, s/search/replacement/).

When either the search pattern or the replacement string contains a slash, it is easier to change the delimiter character rather than escape the slash. Thus, if the pattern was attempting to match UNIX pathnames, which contain slashes, you could choose another character, such as a colon, as the delimiter:

s:/usr/mail:/usr2/mail:

Note that the delimiter appears three times and is required after the replacement. Regardless of which delimiter you use, if it does appear in the regular expression, use a backslash (\) to escape it.

If you don't know what characters the search pattern might have (in a shell program that handles any kind of input, for instance), the safest choice for the delimiter can be a control character. Article 45.35 shows how to make and use a control character as the delimiter in a shell script - without having to store the control character in the file.

You can use any delimiter for a pattern address (not just a slash). Put a backslash (\) before the first delimiter. For example, to delete all lines containing /usr/mail, using a colon (:) as the delimiter:

\:/usr/mail:d

- DD, JP


Previous: 34.6 One Thing at a Time UNIX Power ToolsNext: 34.8 Newlines in a sed Replacement
34.6 One Thing at a Time Book Index34.8 Newlines in a sed Replacement

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