Linux in a Nutshell, Third Edition
by Ellen Siever, Stephen Spainhour, Jessica P. Hekman, Stephen Figgins
Syntax of sed Commands
sed commands have the general form:
[address[,address]][!]command [arguments]
sed commands consist of addresses and editing commands. commands consist of a single letter or symbol; they are described later, alphabetically and by group. arguments include the label supplied to b or t, the filename supplied to r or w, and the substitution flags for s. addresses are described in the next section.
Pattern Addressing
A sed command can specify zero, one, or two addresses. An address can be a line number, the symbol $ (for last line), or a regular expression enclosed in slashes (/ pattern /). Regular expressions are described in Chapter 9. Additionally, \n can be used to match any newline in the pattern space (resulting from the N command) but not the newline at the end of the pattern space.
| If the Command Specifies | Then the Command Is Applied To |
|---|---|
| No address | Each input line. |
| One address |
Any line matching the address. Some commands (a, i, r, q, and =) accept only one address. |
|
Two comma-separated addresses |
First matching line and all succeeding lines up to and including a line matching the second address. |
| An address followed by ! |
All lines that do not match the address. |
Examples
s/xx/yy/g Substitute on all lines (all occurrences) /BSD/d Delete lines containing BSD /^BEGIN/,/^END/p Print between BEGIN and END, inclusive /SAVE/!d Delete any line that doesn't contain SAVE /BEGIN/,/END/!s/xx/yy/g Substitute on all lines, except between BEGIN and END
Braces ({}) are used ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access