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