The stream editor, sed, is a noninteractive editor. It interprets a script and performs the actions in the script. sed is stream-oriented because, like many Unix programs, input flows through the program and is directed to standard output. For example, sort is stream-oriented; vi is not. sed's input typically comes from a file or pipe, but it can also be directed from the keyboard. Output goes to the screen by default but can be captured in a file or sent through a pipe instead.
Typical uses of sed include:
Editing one or more files automatically
Simplifying repetitive edits to multiple files
Writing conversion programs
sed operates as follows:
Each line of input is copied into a pattern space, an internal buffer where editing operations are performed.
All editing commands in a sed script are applied, in order, to each line of input.
Editing commands are applied to all lines (globally) unless line addressing restricts the lines affected.
If a command changes the input, subsequent commands and address tests will be applied to the current line in the pattern space, not the original input line.
The original input file is unchanged because the editing commands modify a copy of each original input line. The copy is sent to standard output (but can be redirected to a file).
sed also maintains the hold space, a separate buffer that can be used to save data for later retrieval.
The syntax for invoking sed has two forms:
sed [-n] [-e] 'command
'file(s)
sed [-n] -fscriptfile file(s)
The first form allows you to specify an editing command on the command line, surrounded by single quotes. The second form allows you to specify a scriptfile, a file containing sed commands. Both forms may be used together, and they may be used multiple times. If no file(s) is specified, sed reads from standard input.
The following options are recognized:
-n
Suppress the default output; sed displays only
those lines specified with the p
command or with
the p
flag of the s
command.
-e
cmd
Next argument is an editing command. Useful if multiple scripts or commands are specified.
file
Next argument is a file containing editing commands.
If the first line of the script is #n
,
sed behaves as if -n
had been
specified.
sed commands have the general form:
[address
[,address
]][!]command
[arguments
]
sed copies each line of input into the pattern space. sed instructions consist of addresses and editing commands. If the address of the command matches the line in the pattern space, then the command is applied to that line. If a command has no address, then it is applied to each input line. If a command changes the contents of the pattern space, subsequent commands and addresses will be applied to the current line in the pattern space, not the original input line.
addresses are described in the next section.
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
.
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 Section 1.3. 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 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. |
Command |
Action performed |
---|---|
|
Substitute on all lines (all occurrences). |
|
Delete lines containing |
|
Print between |
|
Delete any line that doesn't contain
|
|
Substitute on all lines, except between |
Braces ({ }
) are used in sed to
nest one address inside another or to apply multiple commands to the
matched same address.
[/pattern
/[,/pattern
/]]{command1
command2 }
The opening curly brace must end its line, and the closing curly brace must be on a line by itself. Be sure there are no spaces after the braces.
In the lists that follow, the sed commands are grouped by function and are described tersely. Full descriptions, including syntax and examples, can be found afterward in the Section 1.4.4 section.