BSD 4_4 development
[unix-history] / usr / share / man / cat1 / sed.0
CommitLineData
22072751
C
1SED(1) BSD Reference Manual SED(1)
2
3N\bNA\bAM\bME\bE
4 s\bse\bed\bd - stream editor
5
6S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
7 s\bse\bed\bd [-\b-a\ban\bn] _\bc_\bo_\bm_\bm_\ba_\bn_\bd [_\bf_\bi_\bl_\be _\b._\b._\b.]
8 s\bse\bed\bd [-\b-a\ban\bn] [-\b-e\be _\bc_\bo_\bm_\bm_\ba_\bn_\bd] [-\b-f\bf _\bc_\bo_\bm_\bm_\ba_\bn_\bd_\b__\bf_\bi_\bl_\be] [_\bf_\bi_\bl_\be _\b._\b._\b.]
9
10D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
11 The s\bse\bed\bd utility reads the specified files, or the standard input if no
12 files are specified, modifying the input as specified by a list of com-
13 mands. The input is then written to the standard output.
14
15 A single command may be specified as the first argument to s\bse\bed\bd. Multiple
16 commands may be specified by using the -\b-e\be or -\b-f\bf options. All commands
17 are applied to the input in the order they are specified regardless of
18 their origin.
19
20 The following options are available:
21
22 -\b-a\ba The files listed as parameters for the ``w'' functions are creat-
23 ed (or truncated) before any processing begins, by default. The
24 -\b-a\ba option causes s\bse\bed\bd to delay opening each file until a command
25 containing the related ``w'' function is applied to a line of in-
26 put.
27
28 -\b-e\be _\bc_\bo_\bm_\bm_\ba_\bn_\bd
29 Append the editing commands specified by the _\bc_\bo_\bm_\bm_\ba_\bn_\bd argument to
30 the list of commands.
31
32 -\b-f\bf _\bc_\bo_\bm_\bm_\ba_\bn_\bd_\b__\bf_\bi_\bl_\be
33 Append the editing commands found in the file _\bc_\bo_\bm_\bm_\ba_\bn_\bd_\b__\bf_\bi_\bl_\be to the
34 list of commands. The editing commands should each be listed on
35 a separate line.
36
37 -\b-n\bn By default, each line of input is echoed to the standard output
38 after all of the commands have been applied to it. The -\b-n\bn option
39 suppresses this behavior.
40
41 The form of a s\bse\bed\bd command is as follows:
42
43 [address[,address]]function[arguments]
44
45 Whitespace may be inserted before the first address and the function por-
46 tions of the command.
47
48 Normally, s\bse\bed\bd cyclically copies a line of input, not including its termi-
49 nating newline character, into a _\bp_\ba_\bt_\bt_\be_\br_\bn _\bs_\bp_\ba_\bc_\be, (unless there is some-
50 thing left after a ``D'' function), applies all of the commands with ad-
51 dresses that select that pattern space, copies the pattern space to the
52 standard output, appending a newline, and deletes the pattern space.
53
54 Some of the functions use a _\bh_\bo_\bl_\bd _\bs_\bp_\ba_\bc_\be to save all or part of the pattern
55 space for subsequent retrieval.
56
57S\bSe\bed\bd A\bAd\bdd\bdr\bre\bes\bss\bse\bes\bs
58 An address is not required, but if specified must be a number (that
59 counts input lines cumulatively across input files), a dollar (``$'')
60 character that addresses the last line of input, or a context address
61 (which consists of a regular expression preceded and followed by a delim-
62 iter).
63
64 A command line with no addresses selects every pattern space.
65
66
67 A command line with one address selects all of the pattern spaces that
68 match the address.
69
70 A command line with two addresses selects the inclusive range from the
71 first pattern space that matches the first address through the next pat-
72 tern space that matches the second. (If the second address is a number
73 less than or equal to the line number first selected, only that line is
74 selected.) Starting at the first line following the selected range, s\bse\bed\bd
75 starts looking again for the first address.
76
77 Editing commands can be applied to non-selected pattern spaces by use of
78 the exclamation character (``!'') function.
79
80S\bSe\bed\bd R\bRe\beg\bgu\bul\bla\bar\br E\bEx\bxp\bpr\bre\bes\bss\bsi\bio\bon\bns\bs
81 The s\bse\bed\bd regular expressions are basic regular expressions (BRE's, see
82 regex(3) for more information). In addition, s\bse\bed\bd has the following two
83 additions to BRE's:
84
85 1. In a context address, any character other than a backslash (``\'')
86 or newline character may be used to delimit the regular expression.
87 Also, putting a backslash character before the delimiting character
88 causes the character to be treated literally. For example, in the
89 context address \xabc\xdefx, the RE delimiter is an ``x'' and the
90 second ``x'' stands for itself, so that the regular expression is
91 ``abcxdef''.
92
93 2. The escape sequence \n matches a newline character embedded in the
94 pattern space. You can't, however, use a literal newline character
95 in an address or in the substitute command.
96
97 One special feature of s\bse\bed\bd regular expressions is that they can default
98 to the last regular expression used. If a regular expression is empty,
99 i.e. just the delimiter characters are specified, the last regular ex-
100 pression encountered is used instead. The last regular expression is de-
101 fined as the last regular expression used as part of an address or sub-
102 stitute command, and at run-time, not compile-time. For example, the
103 command ``/abc/s//XXX/'' will substitute ``XXX'' for the pattern ``abc''.
104
105S\bSe\bed\bd F\bFu\bun\bnc\bct\bti\bio\bon\bns\bs
106 In the following list of commands, the maximum number of permissible ad-
107 dresses for each command is indicated by [0addr], [1addr], or [2addr],
108 representing zero, one, or two addresses.
109
110 The argument _\bt_\be_\bx_\bt consists of one or more lines. To embed a newline in
111 the text, precede it with a backslash. Other backslashes in text are
112 deleted and the following character taken literally.
113
114 The ``r'' and ``w'' functions take an optional file parameter, which
115 should be separated from the function letter by white space. Each file
116 given as an argument to s\bse\bed\bd is created (or its contents truncated) before
117 any input processing begins.
118
119 The ``b'', ``r'', ``s'', ``t'', ``w'', ``y'', ``!'', and ``:'' functions
120 all accept additional arguments. The following synopses indicate which
121 arguments have to be separated from the function letters by white space
122 characters.
123
124 Two of the functions take a function-list. This is a list of s\bse\bed\bd func-
125 tions separated by newlines, as follows:
126
127 { function
128 function
129 ...
130 function
131 }
132
133 The ``{'' can be preceded by white space and can be followed by white
134 space. The function can be preceded by white space. The terminating
135 ``}'' must be preceded by a newline an optional white space.
136
137 [2addr] function-list
138 Execute function-list only when the pattern space is selected.
139
140 [1addr]a\
141 text
142 Write _\bt_\be_\bx_\bt to standard output immediately before each attempt to
143 read a line of input, whether by executing the ``N'' function or
144 by beginning a new cycle.
145
146 [2addr]b[lable]
147 Branch to the ``:'' function with the specified label. If the
148 label is not specified, branch to the end of the script.
149
150 [2addr]c\
151 text
152 Delete the pattern space. With 0 or 1 address or at the end of a
153 2-address range, _\bt_\be_\bx_\bt is written to the standard output.
154
155 [2addr]d
156 Delete the pattern space and start the next cycle.
157
158 [2addr]D
159 Delete the initial segment of the pattern space through the first
160 newline character and start the next cycle.
161
162 [2addr]g
163 Replace the contents of the pattern space with the contents of
164 the hold space.
165
166 [2addr]G
167 Append a newline character followed by the contents of the hold
168 space to the pattern space.
169
170 [2addr]h
171 Replace the contents of the hold space with the contents of the
172 pattern space.
173
174 [2addr]H
175 Append a newline character followed by the contents of the pat-
176 tern space to the hold space.
177
178 [1addr]i\
179 text
180 Write _\bt_\be_\bx_\bt to the standard output.
181
182 [2addr]l
183 (The letter ell.) Write the pattern space to the standard output
184 in a visually unambiguous form. This form is as follows:
185
186 backslash \
187 alert \a
188 form-feed \f
189 newline \n
190 carriage-return \r
191 tab \t
192 vertical tab \v
193
194 Nonprintable characters are written as three-digit octal numbers
195 (with a preceding backslash) for each byte in the character (most
196 significant byte first). Long lines are folded, with the point
197 of folding indicated by displaying a backslash followed by a new-
198 line. The end of each line is marked with a ``$''.
199
200 [2addr]n
201 Write the pattern space to the standard output if the default
202 output has not been suppressed, and replace the pattern space
203 with the next line of input.
204
205 [2addr]N
206 Append the next line of input to the pattern space, using an em-
207 bedded newline character to separate the appended material from
208 the original contents. Note that the current line number
209 changes.
210
211 [2addr]p
212 Write the pattern space to standard output.
213
214 [2addr]P
215 Write the pattern space, up to the first newline character to the
216 standard output.
217
218 [1addr]q
219 Branch to the end of the script and quit without starting a new
220 cycle.
221
222 [1addr]r file
223 Copy the contents of _\bf_\bi_\bl_\be to the standard output immediately be-
224 fore the next attempt to read a line of input. If _\bf_\bi_\bl_\be cannot be
225 read for any reason, it is silently ignored and no error condi-
226 tion is set.
227
228 [2addr]s/regular expression/replacement/flags
229 Substitute the replacement string for the first instance of the
230 regular expression in the pattern space. Any character other
231 than backslash or newline can be used instead of a slash to de-
232 limit the RE and the replacement. Within the RE and the replace-
233 ment, the RE delimiter itself can be used as a literal character
234 if it is preceded by a backslash.
235
236 An ampersand (``&'') appearing in the replacement is replaced by
237 the string matching the RE. The special meaning of ``&'' in this
238 context can be suppressed by preceding it by backslash. The
239 string ``\#'', where ``#'' is a digit, is replaced by the text
240 matched by the corresponding backreference expression (see
241 re_format(7)).
242
243 A line can be split by substituting a newline character into it.
244 To specify a newline character in the replacement string, precede
245 it with a backslash.
246
247 The value of _\bf_\bl_\ba_\bg_\bs in the substitute function is zero or more of
248 the following:
249
250 0 ... 9
251 Make the substitution only for the N'th occurrence
252 of the regular expression in the pattern space.
253
254 g Make the substitution for all non-overlapping
255 matches of the regular expression, not just the
256 first one.
257
258 p Write the pattern space to standard output if a re-
259 placement was made. If the replacement string is
260 identical to that which it replaces, it is still
261 considered to have been a replacement.
262
263 w _\bf_\bi_\bl_\be Append the pattern space to _\bf_\bi_\bl_\be if a replacement
264 was made. If the replacement string is identical
265 to that which it replaces, it is still considered
266 to have been a replacement.
267
268 [2addr]t [label]
269 Branch to the ``'': function bearing the label if any substitu-
270 tions have been made since the most recent reading of an input
271 line or execution of a ``t'' function. If no label is specified,
272 branch to the end of the script.
273
274 [2addr]w _\bf_\bi_\bl_\be
275 Append the pattern space to the _\bf_\bi_\bl_\be.
276
277 [2addr]x
278 Swap the contents of the pattern and hold spaces.
279
280 [2addr]y/string1/string2/
281 Replace all occurrences of characters in _\bs_\bt_\br_\bi_\bn_\bg_\b1 in the pattern
282 space with the corresponding characters from _\bs_\bt_\br_\bi_\bn_\bg_\b2. Any charac-
283 ter other than a backslash or newline can be used instead of a
284 slash to delimit the strings. Within _\bs_\bt_\br_\bi_\bn_\bg_\b1 and _\bs_\bt_\br_\bi_\bn_\bg_\b2, a
285 backslash followed by any character other than a newline is that
286 literal character, and a backslash followed by an ``n'' is re-
287 placed by a newline character.
288
289 [2addr]!function
290 [2addr]!function-list
291 Apply the function or function-list only to the lines that are
292 _\bn_\bo_\bt selected by the address(es).
293
294 [0addr]:label
295 This function does nothing; it bears a label to which the ``b''
296 and ``t'' commands may branch.
297
298 [1addr]=
299 Write the line number to the standard output followed by a new-
300 line character.
301
302 [0addr]
303 Empty lines are ignored.
304
305 [0addr]#
306 The ``#'' and the remainder of the line are ignored (treated as a
307 comment), with the single exception that if the first two charac-
308 ters in the file are ``#n'', the default output is suppressed.
309 This is the same as specifying the -\b-n\bn option on the command line.
310
311 The s\bse\bed\bd utility exits 0 on success and >0 if an error occurs.
312
313S\bSE\bEE\bE A\bAL\bLS\bSO\bO
314 awk(1), ed(1), grep(1), regex(3), re_format(7)
315
316H\bHI\bIS\bST\bTO\bOR\bRY\bY
317 A s\bse\bed\bd command appeared in Version 7 AT&T UNIX.
318
319S\bST\bTA\bAN\bND\bDA\bAR\bRD\bDS\bS
320 The s\bse\bed\bd function is expected to be a superset of the IEEE Std1003.2
321 (``POSIX'') specification.
322
3234.4BSD June 6, 1993 5