SED(1) COMMAND REFERENCE SED(1)
NAME
sed - stream editor
SYNOPSIS
sed [ -n ] [ -g ] scripts [ filename ... ]
DESCRIPTION
Sed copies the named files (standard input default) to the
standard output, edited according to a script of commands.
The scripts argument is actually a set of arguments which
contain the editing scripts (script) and script files
(sfilename). Scripts can be in one of the following forms :
[-e] script - the script is on the command line
-f sfilename - the script is in the file
Any number of script and script filename arguments can be
given. If there is only one script and it is on the command
line, the -e is optional.
Normally, all input lines are copied to the standard output.
The -n option causes only those lines that are explicitly
printed by the editing commands to be copied.
The -g option causes all occurrences of the substitute
command (see the editing command descriptions) to be global,
as if each has the g flag.
A script consists of comments and editing commands, one per
line. (There is a limit of 500 sed commands per script.)
Comments are lines beginning with the character `#', and are
ignored by sed. Editing commands are of the following form:
[address [, address] ] function [arguments]
In normal operation sed cyclically copies a line of input
into a pattern space (unless there is something left after a
D command), applies in sequence all commands whose addresses
select that pattern space, and at the end of the script
copies the pattern space to the standard output (except
under -n) and deletes the pattern space.
An address is either a decimal number that counts input
lines cumulatively across files, a $ that addresses the last
line of input, or a context address, /regular expression/.
The context address is in the style of ed(1) and is modified
in the following way:
The escape sequence \n matches a newline embedded in
the pattern space.
Printed 10/17/86 1
SED(1) COMMAND REFERENCE SED(1)
A command line with no addresses selects every pattern
space.
A command line with one address selects each pattern space
that matches the address.
A command line with two addresses selects the inclusive
range from the first pattern space that matches the first
address through the next pattern space that matches the
second. (If the second address is a number less than or
equal to the linenumber first selected, only one line is
selected.) Thereafter the process is repeated, looking again
for the first address.
Editing commands can be applied only to nonselected pattern
spaces by use of the negation function ! (below).
An argument denoted text consists of one or more lines, all
but the last of which end with a backslash (\) to hide the
newline. Backslashes in text are treated like backslashes
in the replacement string of an s command, and may be used
to protect initial blanks and tabs against the stripping
that is done on every script line.
An argument denoted rfilename or wfilename must terminate
the command line and must be preceded by exactly one blank.
Each wfilename is created before processing begins.
In the following list of functions the maximum number of
permissible addresses for each function is indicated in
parentheses.
(1) a\
text
Append. Place text on the output before reading the
next input line.
(2) b label
Branch to the colon (:) command bearing the label. If
label is empty, branch to the end of the script.
(2) c\
text
Change. Delete the pattern space. With zero or one
address or at the end of a two-address range, place text
on the output. Start the next cycle.
(2) d
Delete the pattern space. Start the next cycle.
(2) D
Delete the initial segment of the pattern space through
Printed 10/17/86 2
SED(1) COMMAND REFERENCE SED(1)
the first newline. Start the next cycle.
(2) g
Replace the contents of the pattern space by the
contents of the hold space.
(2) G
Append the contents of the hold space to the pattern
space.
(2) h
Replace the contents of the hold space by the contents
of the pattern space.
(2) H
Append the contents of the pattern space to the hold
space.
(1) i\
text
Insert. Place text on the standard output.
(2) n
Copy the pattern space to the standard output. Replace
the pattern space with the next line of input.
(2) N
Append the next line of input to the pattern space with
an embedded newline. (The current line number changes.)
(2) p
Print. Copy the pattern space to the standard output.
(2) P
Copy the initial segment of the pattern space through
the first newline to the standard output.
(1) q
Quit. Branch to the end of the script. Do not start a
new cycle.
(2) r rfilename
Read the contents of rfilename. Place them on the
output before reading the next input line.
(2) s /regular expression/replacement/flags
Substitute the replacement string for instances of the
regular expression in the pattern space. Any character
may be used instead of the slash (/). For a fuller
description see ed(1). Flags is zero or more of the
following:
Printed 10/17/86 3
SED(1) COMMAND REFERENCE SED(1)
g Global. Substitute for all nonoverlapping instances
of the regular expression rather than just the first
one.
p Print the pattern space if a replacement was made.
w wfilename
Write. Append the pattern space to wfilename if a
replacement was made.
(2) t label
Test. Branch to the colon (:) command bearing the label
if any substitutions have been made since the most
recent reading of an input line or execution of a t. If
label is empty, branch to the end of the script.
(2) w wfilename
Write. Append the pattern space to wfilename.
(2) x
Exchange the contents of the pattern and hold spaces.
(2) y /string1/string2/
Transform. Replace all occurrences of characters in
string1 with the corresponding character in string2. The
lengths of string1 and string2 must be equal.
(2) ! function
Don't. Apply the function (or group, if function is {)
only to lines not selected by the address(es).
(0) : label
This command does nothing; it bears a label for b and t
commands to branch to.
(1) =
Place the current linenumber on the standard output as a
line.
(2) {
Execute the following commands through a matching } only
when the pattern space is selected.
(0)
An empty command is ignored.
OPTIONS
-e script
The script is on the command line.
-f sfilename
The script is in sfilename.
Printed 10/17/86 4
SED(1) COMMAND REFERENCE SED(1)
-g Causes all occurrences of the substitute command (see
the editing command descriptions) to be global, as if
each has the g flag.
-n Causes only those lines that are explicitly printed by
the editing commands to be copied.
EXAMPLES
The following example copies the contents of the file orig
to the file new, deleting lines beginning with a and ending
with e:
sed 'd/^a.*e$/' orig > new
This example copies the contents of the file orig.c to the
file new.c, changing all references to system include files
(like #include <stdio.h>) to references to local include
files with the same name (\t represents the tab character):
sed 's/^\(#[ \t]*include[ \t][ \t]*\)<\(.*\)>$/\1"\2"/'
orig.c > new.c
This example copies the contents of the file orig to the
terminal, deleting all blank lines, and also performing the
editing commands in the file example.sed:
sed -e 'd/^[ \t]*$/' -f example.sed orig
The following examples are equivalent. Both copy the
contents of the file orig to the file new, replacing all
occurrences of abc with 123:
sed '1,$s/abc/123/g' orig > new
sed -g '1,$s/abc/123/' orig > new
This example copies the file input to the standard output,
replacing the first occurence of the sequence the with a,
and copying all other lines as is:
Printed 10/17/86 5
SED(1) COMMAND REFERENCE SED(1)
sed 's/the/a/
t found
b
:found
n
b found' input
RETURN VALUE
[NO_ERRS] Command completed without error.
[USAGE] Incorrect command line syntax. Execution
terminated.
[NP_WARN] An error warranting a warning message
occurred. Execution continues.
[NP_ERR] An error occurred that was not a system
error. Execution terminated.
[P_ERR] A system error occurred. Execution
terminated. See intro(2) for more
information on system errors.
CAVEATS
The number of distinct files that can be written by the w
command is limited to the number of files that can be open
in a program at the same (subtract one from this if input is
not from standard input).
SEE ALSO
awk(1), ed(1), grep(1), lex(1).
Printed 10/17/86 6
%%index%%
na:72,55;
sy:127,178;
de:305,2433;2882,2143;5169,1815;7128,1882;
op:9010,237;9391,296;
ex:9687,1382;11213,110;
rv:11323,621;
ca:11944,281;
se:12225,140;
%%index%%000000000186