1SED(1) General Commands Manual SED(1)
2
3
4
6 sed - stream editor
7
9 sed [ -n ] [ -e script ] [ -f sfile ] [ file ] ...
10
12 Sed copies the named files (standard input default) to the standard
13 output, edited according to a script of commands. The -f option causes
14 the script to be taken from file sfile; these options accumulate. If
15 there is just one -e option and no -f's, the flag -e may be omitted.
16 The -n option suppresses the default output.
17
18 A script consists of editing commands, one per line, of the following
19 form:
20
21 [address [, address] ] function [arguments]
22
23 In normal operation sed cyclically copies a line of input into a pat‐
24 tern space (unless there is something left after a `D' command),
25 applies in sequence all commands whose addresses select that pattern
26 space, and at the end of the script copies the pattern space to the
27 standard output (except under -n) and deletes the pattern space.
28
29 An address is either a decimal number that counts input lines cumula‐
30 tively across files, a `$' that addresses the last line of input, or a
31 context address, `/regular expression/', in the style of ed(1) modified
32 thus:
33
34 The escape sequence `\n' matches a newline embedded in the pat‐
35 tern space.
36
37 A command line with no addresses selects every pattern space.
38
39 A command line with one address selects each pattern space that matches
40 the address.
41
42 A command line with two addresses selects the inclusive range from the
43 first pattern space that matches the first address through the next
44 pattern space that matches the second. (If the second address is a
45 number less than or equal to the line number first selected, only one
46 line is selected.) Thereafter the process is repeated, looking again
47 for the first address.
48
49 Editing commands can be applied only to non-selected pattern spaces by
50 use of the negation function `!' (below).
51
52 In the following list of functions the maximum number of permissible
53 addresses for each function is indicated in parentheses.
54
55 An argument denoted text consists of one or more lines, all but the
56 last of which end with `\' to hide the newline. Backslashes in text
57 are treated like backslashes in the replacement string of an `s' com‐
58 mand, and may be used to protect initial blanks and tabs against the
59 stripping that is done on every script line.
60
61 An argument denoted rfile or wfile must terminate the command line and
62 must be preceded by exactly one blank. Each wfile is created before
63 processing begins. There can be at most 10 distinct wfile arguments.
64
65 (1)a\
66 text
67 Append. Place text on the output before reading the next input
68 line.
69
70 (2)b label
71 Branch to the `:' command bearing the label. If label is empty,
72 branch to the end of the script.
73
74 (2)c\
75 text
76 Change. Delete the pattern space. With 0 or 1 address or at
77 the end of a 2-address range, place text on the output. Start
78 the next cycle.
79
80 (2)d Delete the pattern space. Start the next cycle.
81
82 (2)D Delete the initial segment of the pattern space through the
83 first newline. Start the next cycle.
84
85 (2)g Replace the contents of the pattern space by the contents of the
86 hold space.
87
88 (2)G Append the contents of the hold space to the pattern space.
89
90 (2)h Replace the contents of the hold space by the contents of the
91 pattern space.
92
93 (2)H Append the contents of the pattern space to the hold space.
94
95 (1)i\
96 text
97 Insert. Place text on the standard output.
98
99 (2)n Copy the pattern space to the standard output. Replace the pat‐
100 tern space with the next line of input.
101
102 (2)N Append the next line of input to the pattern space with an
103 embedded newline. (The current line number changes.)
104
105 (2)p Print. Copy the pattern space to the standard output.
106
107 (2)P Copy the initial segment of the pattern space through the first
108 newline to the standard output.
109
110 (1)q Quit. Branch to the end of the script. Do not start a new
111 cycle.
112
113 (2)r rfile
114 Read the contents of rfile. Place them on the output before
115 reading the next input line.
116
117 (2)s/regular expression/replacement/flags
118 Substitute the replacement string for instances of the regular
119 expression in the pattern space. Any character may be used
120 instead of `/'. For a fuller description see ed(1). Flags is
121 zero or more of
122
123 g Global. Substitute for all nonoverlapping instances of
124 the regular expression rather than just the first one.
125
126 p Print the pattern space if a replacement was made.
127
128 w wfile
129 Write. Append the pattern space to wfile if a replace‐
130 ment was made.
131
132 (2)t label
133 Test. Branch to the `:' command bearing the label if any sub‐
134 stitutions have been made since the most recent reading of an
135 input line or execution of a `t'. If label is empty, branch to
136 the end of the script.
137
138 (2)w wfile
139 Write. Append the pattern space to wfile.
140
141 (2)x Exchange the contents of the pattern and hold spaces.
142
143 (2)y/string1/string2/
144 Transform. Replace all occurrences of characters in string1
145 with the corresponding character in string2. The lengths of
146 string1 and string2 must be equal.
147
148 (2)! function
149 Don't. Apply the function (or group, if function is `{') only
150 to lines not selected by the address(es).
151
152 (0): label
153 This command does nothing; it bears a label for `b' and `t' com‐
154 mands to branch to.
155
156 (1)= Place the current line number on the standard output as a line.
157
158 (2){ Execute the following commands through a matching `}' only when
159 the pattern space is selected.
160
161 (0) An empty command is ignored.
162
164 ed(1), grep(1), awk(1), lex(1)
165
166
167
1687th Edition April 29, 1985 SED(1)