Commit | Line | Data |
---|---|---|
7701b0dd | 1 | .\" Copyright (c) 1991 The Regents of the University of California. |
94bed826 | 2 | .\" All rights reserved. |
2d33ba58 | 3 | .\" |
ae122740 KB |
4 | .\" This code is derived from software contributed to Berkeley by |
5 | .\" the Institute of Electrical and Electronics Engineers, Inc. | |
6 | .\" | |
7701b0dd | 7 | .\" %sccs.include.redist.roff% |
94bed826 | 8 | .\" |
c3fc0f23 | 9 | .\" @(#)sed.1 6.7 (Berkeley) %G% |
daacd5f0 | 10 | .\" |
7701b0dd CL |
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 n | |
20 | .Ar script | |
21 | .Ar | |
22 | .Nm sed | |
23 | .Op Fl n | |
24 | .Op Fl e Ar script | |
25 | .Op Fl f Ar script_file | |
26 | .Ar | |
27 | .Sh DESCRIPTION | |
2d33ba58 | 28 | The |
7701b0dd CL |
29 | .Nm sed |
30 | utility is a stream editor which reads one or more | |
31 | text files, applies given editing command scripts, | |
32 | and writes the results to standard output. | |
33 | The script of editing commands can be given in the command | |
34 | line, or can be contained in the file | |
35 | .Ar script_file . | |
36 | .Pp | |
37 | Options: | |
38 | .Bl -tag -width indent | |
39 | .It Fl e Ar script | |
40 | The command line | |
41 | .Ar script | |
42 | is used to edit the input. | |
43 | If multiple | |
44 | .Fl e | |
45 | options are given, the scripts are | |
46 | applied in the order given to each line of the | |
47 | input files. | |
48 | If a | |
49 | .Fl f | |
50 | option is given in addition | |
51 | to | |
52 | .Fl e , | |
53 | lines are acted upon by scripts first. | |
54 | .It Fl f Ar script_file | |
55 | The file | |
56 | .Ar script_file | |
57 | is expected to contain editing commands, one per line, | |
58 | and these commands are applied to the input. | |
59 | If multiple | |
60 | .Fl f | |
61 | options are given, the commands in the | |
62 | .Ar script_file Ns s | |
63 | are applied in | |
64 | the order given to each line of the input | |
65 | files. | |
66 | If a | |
67 | .Fl e | |
68 | option is given in addition to | |
69 | .Fl f , | |
70 | lines are acted upon by | |
71 | the commands in the | |
72 | .At script_file Ns s | |
73 | first. | |
74 | .It Fl n | |
2d33ba58 | 75 | The |
7701b0dd CL |
76 | .Fl n |
77 | option suppresses the default output, which normally | |
78 | passes each line, after it is examined for editing, | |
79 | to standard output. | |
80 | Therefore, only lines explicitly | |
81 | selected for output are written. | |
82 | .El | |
83 | .Pp | |
84 | The following operands are available: | |
85 | .Bl -tag -width file | |
86 | .It Ar file | |
87 | A pathname of a file whose contents are read and | |
88 | edited. | |
89 | If multiple file operands are given, | |
90 | the named files are read in the order given and | |
91 | the concatenation is edited. | |
92 | If no file operands | |
93 | are given, the standard input is used. | |
94 | .It Ar script | |
95 | The script consists of one or more editing | |
96 | instructions that are entered on the command line. | |
97 | .El | |
98 | .\" .Pp | |
99 | .\" The following environment variable affects the execution of | |
100 | .\" sed: | |
101 | .\" .It Ev LC_CTYPE | |
102 | .\" The locale for character classification. | |
103 | .Pp | |
104 | .Bd -filled -offset indent -compact | |
105 | .Op address Op ,address | |
106 | function | |
107 | .Op arguments | |
108 | .Ed | |
109 | .Pp | |
110 | In default operation, | |
111 | .Nm sed | |
112 | cyclically copies a line of input | |
113 | into a pattern space (unless there is something left after a | |
114 | .Cm D | |
115 | command), applies in sequence all commands whose addresses | |
116 | select that pattern space, and at the end of the script | |
117 | copies the pattern space to the standard output (except | |
118 | under | |
119 | .Fl n ) | |
2d33ba58 | 120 | and deletes the pattern space. |
7701b0dd CL |
121 | .Pp |
122 | Some of the commands use a hold space to save all or part of | |
123 | the pattern space for | |
124 | subsequent retrieval. | |
125 | .\" The pattern and hold spaces are each | |
126 | .\" limited to | |
127 | .\" .Pf { Dv SED_PATTERN_MAX Ns } | |
128 | .\" bytes. | |
129 | .Pp | |
130 | An address is either no address; a decimal number that | |
131 | counts input lines cumulatively across files; a | |
132 | .Ql $ | |
133 | that | |
134 | addresses the last line of input; a context address; or | |
135 | regular expression. | |
136 | .Pp | |
137 | A command line with no addresses selects every pattern | |
138 | space. | |
139 | .Pp | |
140 | A command line with one address selects each pattern space | |
141 | that matches the address. | |
142 | .Pp | |
143 | A command line with two addresses selects the inclusive | |
144 | range from the first pattern space that matches the first | |
145 | address through the next pattern space which matches the | |
146 | second. | |
147 | (If the second address is a number less than or | |
148 | equal to the line number first selected, only one line is | |
149 | selected.) | |
150 | Starting at the first line following the | |
151 | selected range, | |
152 | .Nm sed | |
153 | looks again for the first address. | |
154 | Thereafter the process is repeated. | |
155 | .Pp | |
2d33ba58 | 156 | Editing commands can be applied only to non-selected pattern |
7701b0dd CL |
157 | spaces by use of the negation function |
158 | .Cm \&! | |
159 | (See below.) | |
160 | .Pp | |
161 | The | |
162 | .Nm sed | |
163 | utility uses basic regular expressions, as are found in the | |
164 | editor | |
165 | .Xr ed 1 , | |
166 | with the following additions: | |
167 | .Pp | |
168 | .Bl -enum -offset indent | |
169 | .It | |
c3fc0f23 KB |
170 | In a context address, the construction |
171 | .Li \e?RE? , | |
172 | where ? | |
173 | is any character, is identical to | |
174 | .Li /RE/ . | |
175 | Note that in the | |
176 | context address | |
177 | .Li \exabc\exdefx , | |
178 | the second | |
179 | .Ql x | |
180 | stands for | |
181 | itself, so that the regular expression is | |
182 | .Li abcxdef . | |
7701b0dd | 183 | .It |
c3fc0f23 KB |
184 | The escape sequence |
185 | .Ql \en | |
186 | matches a <newline> embedded | |
7701b0dd CL |
187 | in the pattern space. |
188 | .It | |
c3fc0f23 KB |
189 | A period |
190 | .Ql \&. | |
191 | matches any character except the terminal <newline> of the pattern space. | |
7701b0dd CL |
192 | .El |
193 | .Pp | |
194 | In the following list of functions the maximum number of | |
195 | permissible addresses for each function is indicated by | |
196 | .Op 0addr , | |
197 | .Op 1addr , | |
2d33ba58 | 198 | or |
7701b0dd CL |
199 | .Op 2addr ; |
200 | representing zero, one, or two | |
201 | addresses. | |
202 | .Pp | |
203 | The argument text consists of one or more lines. | |
204 | Each | |
205 | embedded <newline> in the text shall be preceded by a | |
206 | backslash. | |
207 | Other backslashes in text are treated like | |
208 | backslashes in the replacement string of an s command, and | |
209 | can be used to protect initial <blank>s against the stripping | |
210 | that is done on every script line. | |
211 | .Pp | |
212 | The | |
213 | .Cm r | |
214 | and | |
215 | .Cm w | |
216 | commands take an optional | |
217 | .Ar rfile | |
218 | (or | |
219 | .Ar wfile ) | |
220 | parameter, separated from the command letter by zero or more | |
221 | <blank>s. | |
222 | .Pp | |
223 | The argument | |
224 | .Ar rfile | |
225 | or the argument | |
226 | .Ar wfile | |
227 | shall terminate the | |
228 | command line. | |
2d33ba58 | 229 | Each |
7701b0dd CL |
230 | .Ar wfile |
231 | is created before processing | |
232 | begins. | |
233 | There can be at most ten distinct | |
234 | .Ar wfile | |
235 | arguments | |
236 | in the script. | |
237 | .Pp | |
238 | The | |
239 | .Cm b , r , s , t , | |
240 | .Cm w , y , \&! , | |
241 | and | |
242 | .Cm \&: | |
243 | commands take additional | |
2d33ba58 | 244 | arguments. |
7701b0dd CL |
245 | The following synopses indicate which arguments |
246 | are separated from the commands by <blank>s. | |
247 | .Bl -tag -width addrcommandxx | |
c3fc0f23 KB |
248 | .It Xo |
249 | .Oo Ad 2addr Oc \&{ command_list \&} | |
250 | .Xc | |
7701b0dd CL |
251 | Executes command_list only when the pattern |
252 | space is selected. | |
253 | The {} braces can be preceded | |
254 | and followed by white space. | |
c3fc0f23 KB |
255 | .It Xo |
256 | .Oo Ad 1addr Oc Ns Cm a Ar text | |
257 | .Xc | |
7701b0dd CL |
258 | Writes text to the standard output after the |
259 | pattern space is written. | |
c3fc0f23 KB |
260 | .It Xo |
261 | .Oo Ad 2addr Oc Ns Cm b Ar label | |
262 | .Xc | |
7701b0dd CL |
263 | Branches to the |
264 | .Cm \&: | |
265 | command bearing the label. | |
266 | If label is empty, branch to the end of the | |
267 | script. | |
c3fc0f23 KB |
268 | .It Xo |
269 | .Oo Ad 2addr Oc Ns Cm c Ar text | |
270 | .Xc | |
7701b0dd CL |
271 | Deletes the pattern space. |
272 | With 0 or 1 | |
273 | address or at the end of a 2-address range, | |
274 | places text on the output. | |
c3fc0f23 KB |
275 | .It Xo |
276 | .Oo Ad 2addr Oc Ns Cm d | |
277 | .Xc | |
7701b0dd CL |
278 | Deletes the pattern space and starts the next |
279 | cycle. | |
c3fc0f23 KB |
280 | .It Xo |
281 | .Oo Ad 2addr Oc Ns Cm D | |
282 | .Xc | |
7701b0dd CL |
283 | Deletes the initial segment of the pattern |
284 | space through the first <newline> and starts | |
285 | the next cycle. | |
c3fc0f23 KB |
286 | .It Xo |
287 | .Oo Ad 2addr Oc Ns Cm g | |
288 | .Xc | |
7701b0dd CL |
289 | Replaces the contents of the pattern space by |
290 | the contents of the hold space. | |
c3fc0f23 KB |
291 | .It Xo |
292 | .Oo Ad 2addr Oc Ns Cm G | |
293 | .Xc | |
7701b0dd CL |
294 | Appends the contents of the hold space to the |
295 | pattern space. | |
c3fc0f23 KB |
296 | .It Xo |
297 | .Oo Ad 2addr Oc Ns Cm h | |
298 | .Xc | |
7701b0dd CL |
299 | Replaces the contents of the hold space by |
300 | the contents of the pattern space. | |
c3fc0f23 KB |
301 | .It Xo |
302 | .Oo Ad 2addr Oc Ns Cm H | |
303 | .Xc | |
7701b0dd CL |
304 | Appends the contents of the pattern space to |
305 | the hold space. | |
c3fc0f23 KB |
306 | .It Xo |
307 | .Oo Ad 1addr Oc Ns Cm i Ar text | |
308 | .Xc | |
7701b0dd CL |
309 | Writes text to the standard output before the |
310 | pattern space is written. | |
c3fc0f23 KB |
311 | .It Xo |
312 | .Oo Ad 2addr Oc Ns Cm l | |
313 | .Xc | |
7701b0dd CL |
314 | Lists the pattern space on the standard out- |
315 | put in an unambiguous form. | |
316 | Nonprinting | |
317 | characters are listed as hexadecimal digit | |
318 | pairs, with a preceding backslash, with the | |
319 | following exceptions: | |
c3fc0f23 | 320 | .Bl -column <carriagexreturn> -offset indent |
7701b0dd CL |
321 | <alert> \ea |
322 | <backslash> \e\e | |
323 | <backspace> \eb | |
324 | <carriage return> \er | |
325 | <form-feed> \ef | |
326 | <newline> \en | |
327 | <tab> \et | |
328 | <vertical tab> \ev | |
329 | .El | |
7701b0dd CL |
330 | .Pp |
331 | Long lines are folded; the length at which | |
332 | folding occurs is ungiven, but should be | |
333 | appropriate for the output device. | |
c3fc0f23 KB |
334 | .It Xo |
335 | .Oo Ad 2addr Oc Ns Cm n | |
336 | .Xc | |
7701b0dd CL |
337 | Copies the pattern space to the standard output |
338 | and replaces the pattern space with the | |
339 | next line of input. | |
c3fc0f23 KB |
340 | .It Xo |
341 | .Oo Ad 2addr Oc Ns Cm N | |
342 | .Xc | |
7701b0dd CL |
343 | Appends the next line of input to the pattern |
344 | space, using an embedded <newline> to | |
345 | separate the appended material from the | |
346 | original material. | |
347 | Note that the current line | |
348 | number changes. | |
c3fc0f23 KB |
349 | .It Xo |
350 | .Oo Ad 2addr Oc Ns Cm p | |
351 | .Xc | |
7701b0dd CL |
352 | Copies |
353 | .Op prints | |
354 | the pattern space to the | |
355 | standard output. | |
c3fc0f23 KB |
356 | .It Xo |
357 | .Oo Ad 2addr Oc Ns Cm P | |
358 | .Xc | |
7701b0dd CL |
359 | Copies |
360 | .Op prints | |
361 | the pattern space, up to the | |
362 | first <newline>, to the standard output. | |
c3fc0f23 KB |
363 | .It Xo |
364 | .Oo Ad 1addr Oc Ns Cm q | |
365 | .Xc | |
7701b0dd CL |
366 | Branches to the end of the script and quits |
367 | without starting a new cycle. | |
c3fc0f23 KB |
368 | .It Xo |
369 | .Oo Ad 1addr Oc Ns Cm r Ar rfile | |
370 | .Xc | |
7701b0dd CL |
371 | Read the contents of rfile. |
372 | Place them on the | |
373 | output before reading the next input line. | |
c3fc0f23 KB |
374 | .It Xo |
375 | .Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags | |
376 | .Xc | |
7701b0dd CL |
377 | Substitutes the replacement string for |
378 | instances of the regular expression in the | |
379 | pattern space. | |
380 | Any character can be used | |
381 | instead of | |
382 | .Ql / . | |
383 | The value of flags is zero or | |
384 | more of: | |
385 | .Bl -tag -width Ds | |
386 | .It Ar n | |
c3fc0f23 KB |
387 | n=1-512. Substitutes for the |
388 | .Ar n Ns th | |
7701b0dd CL |
389 | occurrence only of the regular |
390 | expression found within the pattern space. | |
391 | .It Cm g | |
392 | Globally substitutes for all | |
393 | non-overlapping instances of the regular | |
394 | expression rather than just | |
395 | the first one. If both | |
396 | .Cm g | |
397 | and | |
398 | .Cm n | |
399 | are given, | |
400 | .Cm g | |
401 | takes precedence. | |
402 | .It Cm p | |
403 | Prints the pattern space if a | |
404 | replacement was made. | |
405 | .It Cm w Ar wfile | |
406 | Write. Appends the pattern space | |
407 | to | |
408 | .Ar wfile | |
409 | if a replacement was | |
410 | made. | |
411 | .El | |
c3fc0f23 KB |
412 | .It Xo |
413 | .Oo Ad 2addr Oc Ns Cm t Ar label | |
414 | .Xc | |
2d33ba58 | 415 | Test. |
7701b0dd CL |
416 | Branches to the |
417 | .Cm \&: | |
418 | command bearing the | |
419 | label if any substitutions have been made | |
420 | since the most recent reading of an input | |
421 | line or execution of a | |
422 | .Cm t . | |
423 | If label is empty, | |
424 | branches to the end of the script. | |
c3fc0f23 KB |
425 | .It Xo |
426 | .Oo Ad 2addr Oc Ns Cm w Ar wfile | |
427 | .Xc | |
7701b0dd CL |
428 | Appends |
429 | .Op writes | |
430 | the pattern space to | |
431 | .Ar wfile . | |
c3fc0f23 KB |
432 | .It Xo |
433 | .Oo Ad 2addr Oc Ns Cm x | |
434 | .Xc | |
7701b0dd CL |
435 | Exchanges the contents of the pattern and |
436 | hold spaces. | |
c3fc0f23 KB |
437 | .It Xo |
438 | .Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/ | |
439 | .Xc | |
7701b0dd CL |
440 | Replaces all occurrences of collating |
441 | elements in | |
442 | .Ar string1 | |
443 | with the corresponding | |
444 | collating element in | |
445 | .Ar string2 . | |
2d33ba58 | 446 | The lengths of |
7701b0dd | 447 | .Ar string1 |
2d33ba58 | 448 | and |
7701b0dd CL |
449 | .Ar string2 |
450 | shall be equal. | |
c3fc0f23 KB |
451 | .It Xo |
452 | .Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction | |
453 | .Xc | |
7701b0dd CL |
454 | Applies the function (or group, if function |
455 | is {) only to the lines that are not selected | |
456 | by the address(es). | |
c3fc0f23 KB |
457 | .It Xo |
458 | .Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel | |
459 | .Xc | |
7701b0dd CL |
460 | This command does nothing; it bears a label |
461 | for the b and t commands to branch to. | |
c3fc0f23 KB |
462 | .It Xo |
463 | .Oo Ad 1addr Oc Ns Cm \&= | |
464 | .Xc | |
7701b0dd CL |
465 | Places the current line number on the standard |
466 | output as a line with its own line | |
467 | number. | |
c3fc0f23 KB |
468 | .It Xo |
469 | .Oo Ad 0addr Oc | |
470 | .Xc | |
2d33ba58 | 471 | An empty command is ignored. |
c3fc0f23 KB |
472 | .It Xo |
473 | .Oo Ad 0addr Oc Cm # | |
474 | .Xc | |
7701b0dd CL |
475 | If a |
476 | .Cm # | |
477 | appears as the first character on any | |
478 | line of a script file, that entire line is | |
479 | ignored (treated as a comment), with the single | |
480 | exception that if the first line of the | |
481 | script file begins with | |
482 | .Cm Ns #n , | |
483 | the default | |
484 | output is suppressed. | |
485 | .El | |
486 | .Pp | |
487 | The | |
488 | .Nm sed | |
489 | utility exits 0 on success, and >0 if an error occurs. | |
490 | .Pp | |
491 | If one or more of the input (not script) files cannot be | |
492 | opened for reading, | |
493 | .Nm sed | |
494 | continues to process the remaining | |
495 | files. | |
496 | .Sh STANDARDS | |
497 | The | |
498 | .Nm sed | |
499 | utility is expected to be | |
500 | .St -p1003.2 | |
501 | compatible. |