Print this page
11622 clean up rarer mandoc lint warnings
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/man/man1/sed.1.man.txt
+++ new/usr/src/man/man1/sed.1.man.txt
1 1 SED(1) User Commands SED(1)
2 2
3 3
4 4
5 5 NAME
6 6 sed - stream editor
7 7
8 8 SYNOPSIS
9 9 sed [-Ealnr] command [file ...]
10 10 sed [-Ealnr] [-e command] [-f command_file] [-I[extension] |
11 11 -i[extension]] [file ...]
12 12
13 13 DESCRIPTION
14 14 The sed utility reads the specified files, or the standard input if no
15 15 files are specified, modifying the input as specified by a list of
16 16 commands. The input is then written to the standard output.
17 17
18 18 A single command may be specified as the first argument to . Multiple
19 19 commands may be specified by using the -e or -f options. All commands
20 20 are applied to the input in the order they are specified regardless of
21 21 their origin.
22 22
23 23 The following options are available:
24 24
25 25 -E Interpret regular expressions as extended (modern) regular
26 26 expressions rather than basic regular expressions (BRE's). The
27 27 regex(5) manual page fully describes both formats.
28 28
29 29 -a The files listed as parameters for the ``w'' functions are
30 30 created (or truncated) before any processing begins, by default.
31 31 The -a option causes sed to delay opening each file until a
32 32 command containing the related ``w'' function is applied to a
33 33 line of input.
34 34
35 35 -e command
36 36 Append the editing commands specified by the command argument to
37 37 the list of commands.
38 38
39 39 -f command_file
40 40 Append the editing commands found in the file command_file to
41 41 the list of commands. The editing commands should each be
42 42 listed on a separate line.
43 43
44 44 -I[extension]
45 45 Edit files in-place, saving backups if extension was specified.
46 46 It is not recommended to omit saving backups when in-place
47 47 editing files, as you risk corruption or partial content in
48 48 situations where disk space is exhausted, etc.
49 49
50 50 Note that in-place editing with -I still takes place in a single
51 51 continuous line address space covering all files, although each
52 52 file preserves its individuality instead of forming one output
53 53 stream. The line counter is never reset between files, address
54 54 ranges can span file boundaries, and the ``$'' address matches
55 55 only the last line of the last file. (See Sed Addresses . )
56 56 That can lead to unexpected results in many cases of in-place
57 57 editing, where using -i is desired.
58 58
59 59 -i[extension]
60 60 Edit files in-place similarly to -I, but treat each file
61 61 independently from other files. In particular, line numbers in
62 62 each file start at 1, the ``$'' address matches the last line of
63 63 the current file, and address ranges are limited to the current
64 64 file. (See Sed Addresses . ) The net result is as though each
65 65 file were edited by a separate sed instance.
66 66
67 67 -l Make output line buffered.
68 68
69 69 -n By default, each line of input is echoed to the standard output
70 70 after all of the commands have been applied to it. The -n
71 71 option suppresses this behavior.
72 72
73 73 -r Same as -E for compatibility with GNU sed.
74 74
75 75 The form of a sed command is as follows:
76 76
77 77 [address[,address]]function[arguments]
78 78
79 79 Whitespace may be inserted before the first address and the
80 80 function portions of the command.
81 81
82 82 Normally, sed cyclically copies a line of input, not including
83 83 its terminating newline character, into a pattern space, (unless
84 84 there is something left after a ``D'' function), applies all of
85 85 the commands with addresses that select that pattern space,
86 86 copies the pattern space to the standard output, appending a
87 87 newline, and deletes the pattern space.
88 88
89 89 Some of the functions use a hold space to save all or part of
90 90 the pattern space for subsequent retrieval.
91 91
92 92 Sed Addresses
93 93 An address is not required, but if specified must have one of the
94 94 following formats:
95 95
96 96 o a number that counts input lines cumulatively across input files
97 97 (or in each file independently if a -i option is in effect);
98 98
99 99 o a dollar (``$'') character that addresses the last line of input
100 100 (or the last line of the current file if a -i option was
101 101 specified);
102 102
103 103 o a context address that consists of a regular expression preceded
104 104 and followed by a delimiter. The closing delimiter can also
105 105 optionally be followed by the ``I'' character, to indicate that
106 106 the regular expression is to be matched in a case-insensitive
107 107 way.
108 108
109 109 A command line with no addresses selects every pattern space.
110 110
111 111 A command line with one address selects all of the pattern
112 112 spaces that match the address.
113 113
114 114 A command line with two addresses selects an inclusive range.
115 115 This range starts with the first pattern space that matches the
116 116 first address. The end of the range is the next following
117 117 pattern space that matches the second address. If the second
118 118 address is a number less than or equal to the line number first
119 119 selected, only that line is selected. The number in the second
120 120 address may be prefixed with a (``+'') to specify the number of
121 121 lines to match after the first pattern. In the case when the
122 122 second address is a context address, sed does not re-match the
123 123 second address against the pattern space that matched the first
124 124 address. Starting at the first line following the selected
125 125 range, sed starts looking again for the first address.
126 126
127 127 Editing commands can be applied to non-selected pattern spaces
128 128 by use of the exclamation character (``!'') function.
129 129
130 130 Sed Regular Expressions
131 131 The regular expressions used in , by default, are basic regular
132 132 expressions (BREs, see regex(5) for more information), but extended
133 133 (modern) regular expressions can be used instead if the -E flag is
134 134 given. In addition, sed has the following two additions to regular
135 135 expressions:
136 136
137 137
138 138 1. In a context address, any character other than a backslash
139 139 (``\'') or newline character may be used to delimit the regular
140 140 expression. The opening delimiter needs to be preceded by a
141 141 backslash unless it is a slash. For example, the context
142 142 address \xabcx is equivalent to /abc/ . Also, putting a
143 143 backslash character before the delimiting character within the
144 144 regular expression causes the character to be treated literally.
145 145 For example, in the context address \xabc\xdefx , the RE
146 146 delimiter is an ``x'' and the second ``x'' stands for itself, so
147 147 that the regular expression is ``abcxdef''.
148 148
149 149
150 150 2. The escape sequence \n matches a newline character embedded in
151 151 the pattern space. You cannot, however, use a literal newline
152 152 character in an address or in the substitute command.
153 153
154 154 One special feature of sed regular expressions is that they can
155 155 default to the last regular expression used. If a regular
156 156 expression is empty, i.e., just the delimiter characters are
157 157 specified, the last regular expression encountered is used
158 158 instead. The last regular expression is defined as the last
159 159 regular expression used as part of an address or substitute
160 160 command, and at run-time, not compile-time. For example, the
161 161 command ``/abc/s//XXX/'' will substitute ``XXX'' for the pattern
162 162 ``abc''.
163 163
164 164 Sed Functions
165 165 In the following list of commands, the maximum number of permissible
166 166 addresses for each command is indicated by [0addr], [1addr], or
167 167 [2addr], representing zero, one, or two addresses.
168 168
169 169 The argument text consists of one or more lines. To embed a newline in
170 170 the text, precede it with a backslash. Other backslashes in text are
171 171 deleted and the following character taken literally.
172 172
173 173 The ``r'' and ``w'' functions take an optional file parameter, which
174 174 should be separated from the function letter by white space. Each file
175 175 given as an argument to sed is created (or its contents truncated)
176 176 before any input processing begins.
177 177
178 178 The ``b'', ``r'', ``s'', ``t'', ``w'', ``y'', ``!'', and ``:''
179 179 functions all accept additional arguments. The following synopses
180 180 indicate which arguments have to be separated from the function letters
181 181 by white space characters.
182 182
183 183 Two of the functions take a function-list. This is a list of sed
184 184 functions separated by newlines, as follows:
185 185
186 186 { function
187 187 function
188 188 ...
189 189 function
190 190 }
191 191
192 192 The ``{'' can be preceded by white space and can be followed by white
193 193 space. The function can be preceded by white space. The terminating
194 194 ``}'' must be preceded by a newline or optional white space.
195 195
196 196
197 197 [2addr] function-list
198 198 Execute function-list only when the pattern space is selected.
199 199
200 200
201 201 [1addr]a\
202 202
203 203 text Write text to standard output immediately before each attempt to
204 204 read a line of input, whether by executing the ``N'' function or
205 205 by beginning a new cycle.
206 206
207 207
208 208 [2addr]b[label]
209 209 Branch to the ``:'' function with the specified label. If the
210 210 label is not specified, branch to the end of the script.
211 211
212 212
213 213 [2addr]c\
214 214
215 215 text Delete the pattern space. With 0 or 1 address or at the end of
216 216 a 2-address range, text is written to the standard output.
217 217
218 218
219 219 [2addr]d
220 220 Delete the pattern space and start the next cycle.
221 221
222 222
223 223 [2addr]D
224 224 Delete the initial segment of the pattern space through the
225 225 first newline character and start the next cycle.
226 226
227 227
228 228 [2addr]g
229 229 Replace the contents of the pattern space with the contents of
230 230 the hold space.
231 231
232 232
233 233 [2addr]G
234 234 Append a newline character followed by the contents of the hold
235 235 space to the pattern space.
236 236
237 237
238 238 [2addr]h
239 239 Replace the contents of the hold space with the contents of the
240 240 pattern space.
241 241
242 242
243 243 [2addr]H
244 244 Append a newline character followed by the contents of the
245 245 pattern space to the hold space.
246 246
247 247
248 248 [1addr]i\
249 249
250 250 text Write text to the standard output.
251 251
252 252
253 253 [2addr]l
254 254 (The letter ell.) Write the pattern space to the standard
255 255 output in a visually unambiguous form. This form is as follows:
256 256
257 257
258 258 backslash
259 259 \\
260 260
261 261 alert \a
262 262
263 263 form-feed
264 264 \f
265 265
266 266 carriage-return
267 267 \r
268 268
269 269 tab \t
270 270
271 271 vertical tab
272 272 \v
273 273
274 274 Nonprintable characters are written as three-digit octal numbers
275 275 (with a preceding backslash) for each byte in the character
276 276 (most significant byte first). Long lines are folded, with the
277 277 point of folding indicated by displaying a backslash followed by
278 278 a newline. The end of each line is marked with a ``$''.
279 279
280 280
281 281 [2addr]n
282 282 Write the pattern space to the standard output if the default
283 283 output has not been suppressed, and replace the pattern space
284 284 with the next line of input.
285 285
286 286
287 287 [2addr]N
288 288 Append the next line of input to the pattern space, using an
289 289 embedded newline character to separate the appended material
290 290 from the original contents. Note that the current line number
291 291 changes.
292 292
293 293
294 294 [2addr]p
295 295 Write the pattern space to standard output.
296 296
297 297
298 298 [2addr]P
299 299 Write the pattern space, up to the first newline character to
300 300 the standard output.
301 301
302 302
303 303 [1addr]q
304 304 Branch to the end of the script and quit without starting a new
305 305 cycle.
306 306
307 307
308 308 [1addr]r file
309 309 Copy the contents of file to the standard output immediately
310 310 before the next attempt to read a line of input. If file cannot
311 311 be read for any reason, it is silently ignored and no error
312 312 condition is set.
313 313
314 314
315 315 [2addr]s/regular expression/replacement/flags
316 316 Substitute the replacement string for the first instance of the
317 317 regular expression in the pattern space. Any character other
318 318 than backslash or newline can be used instead of a slash to
319 319 delimit the RE and the replacement. Within the RE and the
320 320 replacement, the RE delimiter itself can be used as a literal
321 321 character if it is preceded by a backslash.
322 322
323 323 An ampersand (``&'') appearing in the replacement is replaced by
324 324 the string matching the RE. The special meaning of ``&'' in
325 325 this context can be suppressed by preceding it by a backslash.
326 326 The string ``\#'', where ``#'' is a digit, is replaced by the
327 327 text matched by the corresponding backreference expression (see
328 328 regex(5)) .
329 329
330 330 A line can be split by substituting a newline character into it.
331 331 To specify a newline character in the replacement string,
332 332 precede it with a backslash.
333 333
334 334 The value of flags in the substitute function is zero or more of
335 335 the following:
336 336
337 337 N Make the substitution only for the N'th occurrence of the
338 338 regular expression in the pattern space.
339 339
340 340 g Make the substitution for all non-overlapping matches of the
341 341 regular expression, not just the first one.
342 342
343 343 p Write the pattern space to standard output if a replacement was
344 344 made. If the replacement string is identical to that which it
345 345 replaces, it is still considered to have been a replacement.
346 346
347 347 w file Append the pattern space to file if a replacement was made. If
348 348 the replacement string is identical to that which it replaces,
349 349 it is still considered to have been a replacement.
350 350
351 351 I Match the regular expression in a case-insensitive way.
352 352
353 353
354 354 [2addr]t [label]
355 355 Branch to the ``:'' function bearing the label if any
356 356 substitutions have been made since the most recent reading of an
357 357 input line or execution of a ``t'' function. If no label is
358 358 specified, branch to the end of the script.
359 359
360 360
361 361 [2addr]w file
362 362 Append the pattern space to the file.
363 363
364 364
365 365 [2addr]x
366 366 Swap the contents of the pattern and hold spaces.
367 367
368 368
369 369 [2addr]y/string1/string2/
370 370 Replace all occurrences of characters in string1 in the pattern
371 371 space with the corresponding characters from string2. Any
372 372 character other than a backslash or newline can be used instead
373 373 of a slash to delimit the strings. Within string1 and string2,
374 374 a backslash followed by any character other than a newline is
375 375 that literal character, and a backslash followed by an ``n'' is
376 376 replaced by a newline character.
377 377
378 378
379 379 [2addr]!function
380 380
381 381 [2addr]!function-list
382 382 Apply the function or function-list only to the lines that are
383 383 not selected by the address(es).
384 384
385 385
386 386 [0addr]:label
387 387 This function does nothing; it bears a label to which the ``b''
388 388 and ``t'' commands may branch.
389 389
390 390
391 391 [1addr]=
392 392 Write the line number to the standard output followed by a
393 393 newline character.
394 394
395 395
396 396 [0addr]
397 397 Empty lines are ignored.
398 398
399 399
400 400 [0addr]#
401 401 The ``#'' and the remainder of the line are ignored (treated as
402 402 a comment), with the single exception that if the first two
403 403 characters in the file are ``#n'', the default output is
404 404 suppressed. This is the same as specifying the -n option on the
405 405 command line.
406 406
407 407 ENVIRONMENT
408 408 The COLUMNS,LANG,LC_ALL,LC_CTYPE and LC_COLLATE environment variables
409 409 affect the execution of sed as described in environ(5).
410 410
411 411 EXIT STATUS
412 412 The sed utility exits 0 on success, and >0 if an error occurs.
413 413
414 414 SEE ALSO
415 415 awk(1), ed(1), grep(1), regex(5)
416 416
417 417 STANDARDS
418 418 The sed utility is expected to be a superset of the IEEE Std 1003.2
419 419 (``POSIX.2'') specification.
420 420
421 421 The -E, I , a and -i options, the prefixing ``+'' in the second member
422 422 of an address range, as well as the ``I'' flag to the address regular
423 423 expression and substitution command are non-standard extensions and may
424 424 not be available on other operating systems.
425 425
426 426 HISTORY
427 427 A sed command, written by L. E. McMahon, appeared in Version 7 AT&T
428 428 UNIX.
429 429
430 430 AUTHORS
431 431 "Diomidis D. Spinellis" <dds@FreeBSD.org>
432 432
433 433 BUGS
434 434 Multibyte characters containing a byte with value 0x5C (ASCII `\') may
435 435 be incorrectly treated as line continuation characters in arguments to
436 436 the ``a'', ``c'' and ``i'' commands. Multibyte characters cannot be
437 437 used as delimiters with the ``s'' and ``y'' commands.
438 438
439 439
440 440
441 441 February 14, 2015 SED(1)
↓ open down ↓ |
441 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX