Commit | Line | Data |
---|---|---|
c2199a45 AM |
1 | .\" Copyright (c) 1992 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" the Institute of Electrical and Electronics Engineers, Inc. | |
6 | .\" | |
7 | .\" Redistribution and use in source and binary forms, with or without | |
8 | .\" modification, are permitted provided that the following conditions | |
9 | .\" are met: | |
10 | .\" 1. Redistributions of source code must retain the above copyright | |
11 | .\" notice, this list of conditions and the following disclaimer. | |
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
13 | .\" notice, this list of conditions and the following disclaimer in the | |
14 | .\" documentation and/or other materials provided with the distribution. | |
15 | .\" 3. All advertising materials mentioning features or use of this software | |
16 | .\" must display the following acknowledgement: | |
17 | .\" This product includes software developed by the University of | |
18 | .\" California, Berkeley and its contributors. | |
19 | .\" 4. Neither the name of the University nor the names of its contributors | |
20 | .\" may be used to endorse or promote products derived from this software | |
21 | .\" without specific prior written permission. | |
22 | .\" | |
23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | .\" SUCH DAMAGE. | |
34 | .\" | |
35 | .\" @(#)sed.1 5.2 (Berkeley) 8/24/92 | |
36 | .\" | |
37 | .Dd "August 24, 1992" | |
38 | .Dt SED 1 | |
39 | .Os | |
40 | .Sh NAME | |
41 | .Nm sed | |
42 | .Nd stream editor | |
43 | .Sh SYNOPSIS | |
44 | .Nm sed | |
45 | .Op Fl an | |
46 | .Ar command | |
47 | .Op Ar file ... | |
48 | .Nm sed | |
49 | .Op Fl an | |
50 | .Op Fl e Ar command | |
51 | .Op Fl f Ar command_file | |
52 | .Op Ar file ... | |
53 | .Sh DESCRIPTION | |
54 | The | |
55 | .Nm sed | |
56 | utility reads the specified files, or the standard input if no files | |
57 | are specified, modifying the input as specified by a list of commands. | |
58 | The input is then written to the standard output. | |
59 | .Pp | |
60 | A single command may be specified as the first argument to | |
61 | .Nm sed . | |
62 | Multiple commands may be specified by using the | |
63 | .Fl e | |
64 | or | |
65 | .Fl f | |
66 | options. | |
67 | All commands are applied to the input in the order they are specified | |
68 | regardless of their origin. | |
69 | .Pp | |
70 | The following options are available: | |
71 | .Bl -tag -width indent | |
72 | .It Fl a | |
73 | The files listed as parameters for the | |
74 | .Dq w | |
75 | functions are created (or truncated) before any processing begins, | |
76 | by default. | |
77 | The | |
78 | .Fl a | |
79 | option causes | |
80 | .Nm sed | |
81 | to delay opening each file until a command containing the related | |
82 | .Dq w | |
83 | function is applied to a line of input. | |
84 | .It Fl e Ar command | |
85 | Append the editing commands specified by the | |
86 | .Ar command | |
87 | argument | |
88 | to the list of commands. | |
89 | .It Fl f Ar command_file | |
90 | Append the editing commands found in the file | |
91 | .Ar command_file | |
92 | to the list of commands. | |
93 | The editing commands should each be listed on a separate line. | |
94 | .It Fl n | |
95 | By default, each line of input is echoed to the standard output after | |
96 | all of the commands have been applied to it. | |
97 | The | |
98 | .Fl n | |
99 | option suppresses this behavior. | |
100 | .El | |
101 | .Pp | |
102 | The form of a | |
103 | .Nm sed | |
104 | command is as follows: | |
105 | .sp | |
106 | .Dl [address[,address]]function[arguments] | |
107 | .sp | |
108 | Whitespace may be inserted before the first address and the function | |
109 | portions of the command. | |
110 | .Pp | |
111 | Normally, | |
112 | .Nm sed | |
113 | cyclically copies a line of input, not including its terminating newline | |
114 | character, into a | |
115 | .Em "pattern space" , | |
116 | (unless there is something left after a | |
117 | .Dq D | |
118 | function), | |
119 | applies all of the commands with addresses that select that pattern space, | |
120 | copies the pattern space to the standard output, appending a newline, and | |
121 | deletes the pattern space. | |
122 | .Pp | |
123 | Some of the functions use a | |
124 | .Em "hold space" | |
125 | to save all or part of the pattern space for subsequent retrieval. | |
126 | .Sh "Sed Addresses" | |
127 | An address is not required, but if specified must be a number (that counts | |
128 | input lines | |
129 | cumulatively across input files), a dollar | |
130 | .Po | |
131 | .Dq $ | |
132 | .Pc | |
133 | character that addresses the last line of input, or a context address | |
134 | (which consists of a regular expression preceded and followed by a | |
135 | delimiter). | |
136 | .Pp | |
137 | A command line with no addresses selects every pattern space. | |
138 | .Pp | |
139 | A command line with one address selects all of the pattern spaces | |
140 | that match the address. | |
141 | .Pp | |
142 | A command line with two addresses selects the inclusive range from | |
143 | the first pattern space that matches the first address through the next | |
144 | pattern space that matches the second. | |
145 | (If the second address is a number less than or equal to the line number | |
146 | first selected, only that line is selected.) | |
147 | Starting at the first line following the selected range, | |
148 | .Nm sed | |
149 | starts looking again for the first address. | |
150 | .Pp | |
151 | Editing commands can be applied to non-selected pattern spaces by use | |
152 | of the exclamation character | |
153 | .Po | |
154 | .Dq ! | |
155 | .Pc | |
156 | function. | |
157 | .Sh "Sed Regular Expressions" | |
158 | The | |
159 | .Nm sed | |
160 | regular expressions are basic regular expressions (BRE's, see | |
161 | .Xr regex 3 | |
162 | for more information). | |
163 | In addition, | |
164 | .Nm sed | |
165 | has the following two additions to BRE's: | |
166 | .sp | |
167 | .Bl -enum -compact | |
168 | .It | |
169 | In a context address, any character other than a backslash | |
170 | .Po | |
171 | .Dq \e | |
172 | .Pc | |
173 | or newline character may be used to delimit the regular expression. | |
174 | Also, putting a backslash character before the delimiting character | |
175 | causes the character to be treated literally. | |
176 | For example, in the context address \exabc\exdefx, the RE delimiter | |
177 | is an | |
178 | .Dq x | |
179 | and the second | |
180 | .Dq x | |
181 | stands for itself, so that the regular expression is | |
182 | .Dq abcxdef . | |
183 | .sp | |
184 | .It | |
185 | The escape sequence \en matches a newline character embedded in the | |
186 | pattern space. | |
187 | You can't, however, use a literal newline character in an address or | |
188 | in the substitute command. | |
189 | .El | |
190 | .Pp | |
191 | One special feature of | |
192 | .Nm sed | |
193 | regular expressions is that they can default to the last regular | |
194 | expression used. | |
195 | If a regular expression is empty, i.e. just the delimiter characters | |
196 | are specified, the last regular expression encountered is used instead. | |
197 | The last regular expression is defined as the last regular expression | |
198 | used as part of an address or substitute command, and at run-time, not | |
199 | compile-time. | |
200 | For example, the command | |
201 | .Dq /abc/s//XXX/ | |
202 | will substitute | |
203 | .Dq XXX | |
204 | for the pattern | |
205 | .Dq abc . | |
206 | .Sh "Sed Functions" | |
207 | In the following list of commands, the maximum number of permissible | |
208 | addresses for each command is indicated by [0addr], [1addr], or [2addr], | |
209 | representing zero, one, or two addresses. | |
210 | .Pp | |
211 | The argument | |
212 | .Em text | |
213 | consists of one or more lines. | |
214 | To embed a newline in the text, precede it with a backslash. | |
215 | Other backslashes in text are deleted and the following character | |
216 | taken literally. | |
217 | .Pp | |
218 | The | |
219 | .Dq r | |
220 | and | |
221 | .Dq w | |
222 | functions take an optional file parameter, which should be separated | |
223 | from the function letter by white space. | |
224 | Each file given as an argument to | |
225 | .Nm sed | |
226 | is created (or its contents truncated) before any input processing begins. | |
227 | .Pp | |
228 | The | |
229 | .Dq b , | |
230 | .Dq r , | |
231 | .Dq s , | |
232 | .Dq t , | |
233 | .Dq w , | |
234 | .Dq y , | |
235 | .Dq ! , | |
236 | and | |
237 | .Dq \&: | |
238 | functions all accept additional arguments. | |
239 | The following synopses indicate which arguments have to be separated from | |
240 | the function letters by white space characters. | |
241 | .Pp | |
242 | Two of the functions take a function-list. | |
243 | This is a list of | |
244 | .Nm sed | |
245 | functions separated by newlines, as follows: | |
246 | .Bd -literal -offset indent | |
247 | { function | |
248 | function | |
249 | ... | |
250 | function | |
251 | } | |
252 | .Ed | |
253 | .Pp | |
254 | The | |
255 | .Dq { | |
256 | can be preceded by white space and can be followed by white space. | |
257 | The function can be preceded by white space. | |
258 | The terminating | |
259 | .Dq } | |
260 | must be preceded by a newline an optional white space. | |
261 | .sp | |
262 | .Bl -tag -width "XXXXXX" -compact | |
263 | .It [2addr] function-list | |
264 | Execute function-list only when the pattern space is selected. | |
265 | .sp | |
266 | .It [1addr]a\e | |
267 | .It text | |
268 | .br | |
269 | Write | |
270 | .Em text | |
271 | to standard output immediately before each attempt to read a line of input, | |
272 | whether by executing the | |
273 | .Dq N | |
274 | function or by beginning a new cycle. | |
275 | .sp | |
276 | .It [2addr]b[lable] | |
277 | Branch to the | |
278 | .Dq \&: | |
279 | function with the specified label. | |
280 | If the label is not specified, branch to the end of the script. | |
281 | .sp | |
282 | .It [2addr]c\e | |
283 | .It text | |
284 | .br | |
285 | Delete the pattern space. | |
286 | With 0 or 1 address or at the end of a 2-address range, | |
287 | .Em text | |
288 | is written to the standard output. | |
289 | .sp | |
290 | .It [2addr]d | |
291 | Delete the pattern space and start the next cycle. | |
292 | .sp | |
293 | .It [2addr]D | |
294 | Delete the initial segment of the pattern space through the first | |
295 | newline character and start the next cycle. | |
296 | .sp | |
297 | .It [2addr]g | |
298 | Replace the contents of the pattern space with the contents of the | |
299 | hold space. | |
300 | .sp | |
301 | .It [2addr]G | |
302 | Append a newline character followed by the contents of the hold space | |
303 | to the pattern space. | |
304 | .sp | |
305 | .It [2addr]h | |
306 | Replace the contents of the hold space with the contents of the | |
307 | pattern space. | |
308 | .sp | |
309 | .It [2addr]H | |
310 | Append a newline character followed by the contents of the pattern space | |
311 | to the hold space. | |
312 | .sp | |
313 | .It [1addr]i\e | |
314 | .It text | |
315 | .br | |
316 | Write | |
317 | .Em text | |
318 | to the standard output. | |
319 | .sp | |
320 | .It [2addr]l | |
321 | (The letter ell.) | |
322 | Write the pattern space to the standard output in a visually unambiguous | |
323 | form. | |
324 | This form is as follows: | |
325 | .sp | |
326 | .Bl -tag -width "carriage-returnXX" -offset indent -compact | |
327 | .It backslash | |
328 | \e | |
329 | .It alert | |
330 | \ea | |
331 | .It form-feed | |
332 | \ef | |
333 | .It newline | |
334 | \en | |
335 | .It carriage-return | |
336 | \er | |
337 | .It tab | |
338 | \et | |
339 | .It vertical tab | |
340 | \ev | |
341 | .El | |
342 | .Pp | |
343 | Nonprintable characters are written as three-digit octal numbers (with a | |
344 | preceding backslash) for each byte in the character (most significant byte | |
345 | first). | |
346 | Long lines are folded, with the point of folding indicated by displaying | |
347 | a backslash followed by a newline. | |
348 | The end of each line is marked with a | |
349 | .Dq $ . | |
350 | .sp | |
351 | .It [2addr]n | |
352 | Write the pattern space to the standard output if the default output has | |
353 | not been suppressed, and replace the pattern space with the next line of | |
354 | input. | |
355 | .sp | |
356 | .It [2addr]N | |
357 | Append the next line of input to the pattern space, using an embedded | |
358 | newline character to separate the appended material from the original | |
359 | contents. | |
360 | Note that the current line number changes. | |
361 | .sp | |
362 | .It [2addr]p | |
363 | Write the pattern space to standard output. | |
364 | .sp | |
365 | .It [2addr]P | |
366 | Write the pattern space, up to the first newline character to the | |
367 | standard output. | |
368 | .sp | |
369 | .It [1addr]q | |
370 | Branch to the end of the script and quit without starting a new cycle. | |
371 | .sp | |
372 | .It [1addr]r file | |
373 | Copy the contents of | |
374 | .Em file | |
375 | to the standard output immediately before the next attempt to read a | |
376 | line of input. | |
377 | If | |
378 | .Em file | |
379 | cannot be read for any reason, it is silently ignored and no error | |
380 | condition is set. | |
381 | .sp | |
382 | .It [2addr]s/regular expression/replacement/flags | |
383 | Substitute the replacement string for the first instance of the regular | |
384 | expression in the pattern space. | |
385 | Any character other than backslash or newline can be used instead of | |
386 | a slash to delimit the RE and the replacement. | |
387 | Within the RE and the replacement, the RE delimiter itself can be used as | |
388 | a literal character if it is preceded by a backslash. | |
389 | .Pp | |
390 | An ampersand | |
391 | .Po | |
392 | .Dq & | |
393 | .Pc | |
394 | appearing in the replacement is replaced by the string matching the RE. | |
395 | The special meaning of | |
396 | .Dq & | |
397 | in this context can be suppressed by preceding it by backslash. | |
398 | The string | |
399 | .Dq \e# , | |
400 | where | |
401 | .Dq # | |
402 | is a digit, is replaced by the text matched | |
403 | by the corresponding backreference expression (see | |
404 | .Xr re_format 7 ). | |
405 | .Pp | |
406 | A line can be split by substituting a newline character into it. | |
407 | To specify a newline character in the replacement string, precede it with | |
408 | a backslash. | |
409 | .Pp | |
410 | The value of | |
411 | .Em flags | |
412 | in the substitute function is zero or more of the following: | |
413 | .Bl -tag -width "XXXXXX" -offset indent | |
414 | .It "0 ... 9" | |
415 | Make the substitution only for the N'th occurrence of the regular | |
416 | expression in the pattern space. | |
417 | .It g | |
418 | Make the substitution for all non-overlapping matches of the | |
419 | regular expression, not just the first one. | |
420 | .It p | |
421 | Write the pattern space to standard output if a replacement was made. | |
422 | If the replacement string is identical to that which it replaces, it | |
423 | is still considered to have been a replacement. | |
424 | .It w Em file | |
425 | Append the pattern space to | |
426 | .Em file | |
427 | if a replacement was made. | |
428 | If the replacement string is identical to that which it replaces, it | |
429 | is still considered to have been a replacement. | |
430 | .El | |
431 | .sp | |
432 | .It [2addr]t [label] | |
433 | Branch to the | |
434 | .Dq : | |
435 | function bearing the label if any substitutions have been made since the | |
436 | most recent reading of an input line or execution of a | |
437 | .Dq t | |
438 | function. | |
439 | If no label is specified, branch to the end of the script. | |
440 | .sp | |
441 | .It [2addr]w Em file | |
442 | Append the pattern space to the | |
443 | .Em file . | |
444 | .sp | |
445 | .It [2addr]x | |
446 | Swap the contents of the pattern and hold spaces. | |
447 | .sp | |
448 | .It [2addr]y/string1/string2/ | |
449 | Replace all occurrences of characters in | |
450 | .Em string1 | |
451 | in the pattern space with the corresponding characters from | |
452 | .Em string2 . | |
453 | Any character other than a backslash or newline can be used instead of | |
454 | a slash to delimit the strings. | |
455 | Within | |
456 | .Em string1 | |
457 | and | |
458 | .Em string2 , | |
459 | the delimiter itself can be used as a literal character if it is preceded | |
460 | by a backslash. | |
461 | .sp | |
462 | .It [2addr]!function | |
463 | .It [2addr]!function-list | |
464 | Apply the function or function-list only to the lines that are | |
465 | .Em not | |
466 | selected by the address(es). | |
467 | .sp | |
468 | .It [0addr]:label | |
469 | This function does nothing; it bears a label to which the | |
470 | .Dq b | |
471 | and | |
472 | .Dq t | |
473 | commands may branch. | |
474 | .sp | |
475 | .It [1addr]= | |
476 | Write the line number to the standard output followed by a newline | |
477 | character. | |
478 | .sp | |
479 | .It [0addr] | |
480 | Empty lines are ignored. | |
481 | .sp | |
482 | .It [0addr]# | |
483 | The | |
484 | .Dq # | |
485 | and the remainder of the line are ignored (treated as a comment), with | |
486 | the single exception that if the first two characters in the file are | |
487 | .Dq #n , | |
488 | the default output is suppressed. | |
489 | This is the same as specifying the | |
490 | .Fl n | |
491 | option on the command line. | |
492 | .El | |
493 | .Pp | |
494 | The | |
495 | .Nm sed | |
496 | utility exits 0 on success and >0 if an error occurs. | |
497 | .Sh SEE ALSO | |
498 | .Xr awk 1 , | |
499 | .Xr ed 1 , | |
500 | .Xr grep 1 , | |
501 | .Xr regex 3 , | |
502 | .Xr re_format 7 | |
503 | .Sh HISTORY | |
504 | A | |
505 | .Nm sed | |
506 | command appeared in | |
507 | .At v7 . | |
508 | .Sh STANDARDS | |
509 | The | |
510 | .Nm sed | |
511 | function is expected to be a superset of the | |
512 | .St -p1003.2 | |
513 | specification. |