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 | .\" |
ae122740 | 9 | .\" @(#)sed.1 6.6 (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 | |
170 | In a context address, the construction \e?RE?, where ? | |
171 | is any character, is identical to /RE/. Note that in the | |
172 | context address \exabc\exdefx, the second x stands for | |
173 | itself, so that the regular expression is abcxdef. | |
174 | .It | |
175 | The escape sequence \en matches a <newline> embedded | |
176 | in the pattern space. | |
177 | .It | |
178 | A period (.) matches any character except the termi- | |
179 | nal <newline> of the pattern space. | |
180 | .El | |
181 | .Pp | |
182 | In the following list of functions the maximum number of | |
183 | permissible addresses for each function is indicated by | |
184 | .Op 0addr , | |
185 | .Op 1addr , | |
2d33ba58 | 186 | or |
7701b0dd CL |
187 | .Op 2addr ; |
188 | representing zero, one, or two | |
189 | addresses. | |
190 | .Pp | |
191 | The argument text consists of one or more lines. | |
192 | Each | |
193 | embedded <newline> in the text shall be preceded by a | |
194 | backslash. | |
195 | Other backslashes in text are treated like | |
196 | backslashes in the replacement string of an s command, and | |
197 | can be used to protect initial <blank>s against the stripping | |
198 | that is done on every script line. | |
199 | .Pp | |
200 | The | |
201 | .Cm r | |
202 | and | |
203 | .Cm w | |
204 | commands take an optional | |
205 | .Ar rfile | |
206 | (or | |
207 | .Ar wfile ) | |
208 | parameter, separated from the command letter by zero or more | |
209 | <blank>s. | |
210 | .Pp | |
211 | The argument | |
212 | .Ar rfile | |
213 | or the argument | |
214 | .Ar wfile | |
215 | shall terminate the | |
216 | command line. | |
2d33ba58 | 217 | Each |
7701b0dd CL |
218 | .Ar wfile |
219 | is created before processing | |
220 | begins. | |
221 | There can be at most ten distinct | |
222 | .Ar wfile | |
223 | arguments | |
224 | in the script. | |
225 | .Pp | |
226 | The | |
227 | .Cm b , r , s , t , | |
228 | .Cm w , y , \&! , | |
229 | and | |
230 | .Cm \&: | |
231 | commands take additional | |
2d33ba58 | 232 | arguments. |
7701b0dd CL |
233 | The following synopses indicate which arguments |
234 | are separated from the commands by <blank>s. | |
235 | .Bl -tag -width addrcommandxx | |
236 | .It Oo Ad 2addr Oc \&{ command_list \&} | |
237 | Executes command_list only when the pattern | |
238 | space is selected. | |
239 | The {} braces can be preceded | |
240 | and followed by white space. | |
241 | .It Oo Ad 1addr Oc Ns Cm a Ar text | |
242 | Writes text to the standard output after the | |
243 | pattern space is written. | |
244 | .It Oo Ad 2addr Oc Ns Cm b Ar label | |
245 | Branches to the | |
246 | .Cm \&: | |
247 | command bearing the label. | |
248 | If label is empty, branch to the end of the | |
249 | script. | |
250 | .It Oo Ad 2addr Oc Ns Cm c Ar text | |
251 | Deletes the pattern space. | |
252 | With 0 or 1 | |
253 | address or at the end of a 2-address range, | |
254 | places text on the output. | |
255 | .It Oo Ad 2addr Oc Ns Cm d | |
256 | Deletes the pattern space and starts the next | |
257 | cycle. | |
258 | .It Oo Ad 2addr Oc Ns Cm D | |
259 | Deletes the initial segment of the pattern | |
260 | space through the first <newline> and starts | |
261 | the next cycle. | |
262 | .It Oo Ad 2addr Oc Ns Cm g | |
263 | Replaces the contents of the pattern space by | |
264 | the contents of the hold space. | |
265 | .It Oo Ad 2addr Oc Ns Cm G | |
266 | Appends the contents of the hold space to the | |
267 | pattern space. | |
268 | .It Oo Ad 2addr Oc Ns Cm h | |
269 | Replaces the contents of the hold space by | |
270 | the contents of the pattern space. | |
271 | .It Oo Ad 2addr Oc Ns Cm H | |
272 | Appends the contents of the pattern space to | |
273 | the hold space. | |
274 | .It Oo Ad 1addr Oc Ns Cm i Ar text | |
275 | Writes text to the standard output before the | |
276 | pattern space is written. | |
277 | .It Oo Ad 2addr Oc Ns Cm l | |
278 | Lists the pattern space on the standard out- | |
279 | put in an unambiguous form. | |
280 | Nonprinting | |
281 | characters are listed as hexadecimal digit | |
282 | pairs, with a preceding backslash, with the | |
283 | following exceptions: | |
284 | .Pp | |
285 | .ne 8 | |
286 | .Bd -filled -offset indent -compact | |
287 | .Bl -column <carriagexreturn> | |
288 | <alert> \ea | |
289 | <backslash> \e\e | |
290 | <backspace> \eb | |
291 | <carriage return> \er | |
292 | <form-feed> \ef | |
293 | <newline> \en | |
294 | <tab> \et | |
295 | <vertical tab> \ev | |
296 | .El | |
297 | .Ed | |
298 | .Pp | |
299 | Long lines are folded; the length at which | |
300 | folding occurs is ungiven, but should be | |
301 | appropriate for the output device. | |
302 | .It Oo Ad 2addr Oc Ns Cm n | |
303 | Copies the pattern space to the standard output | |
304 | and replaces the pattern space with the | |
305 | next line of input. | |
306 | .It Oo Ad 2addr Oc Ns Cm N | |
307 | Appends the next line of input to the pattern | |
308 | space, using an embedded <newline> to | |
309 | separate the appended material from the | |
310 | original material. | |
311 | Note that the current line | |
312 | number changes. | |
313 | .It Oo Ad 2addr Oc Ns Cm p | |
314 | Copies | |
315 | .Op prints | |
316 | the pattern space to the | |
317 | standard output. | |
318 | .It Oo Ad 2addr Oc Ns Cm P | |
319 | Copies | |
320 | .Op prints | |
321 | the pattern space, up to the | |
322 | first <newline>, to the standard output. | |
323 | .It Oo Ad 1addr Oc Ns Cm q | |
324 | Branches to the end of the script and quits | |
325 | without starting a new cycle. | |
326 | .It Oo Ad 1addr Oc Ns Cm r Ar rfile | |
327 | Read the contents of rfile. | |
328 | Place them on the | |
329 | output before reading the next input line. | |
330 | .It Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags | |
331 | Substitutes the replacement string for | |
332 | instances of the regular expression in the | |
333 | pattern space. | |
334 | Any character can be used | |
335 | instead of | |
336 | .Ql / . | |
337 | The value of flags is zero or | |
338 | more of: | |
339 | .Bl -tag -width Ds | |
340 | .It Ar n | |
341 | n=1-512. Substitutes for the nth | |
342 | occurrence only of the regular | |
343 | expression found within the pattern space. | |
344 | .It Cm g | |
345 | Globally substitutes for all | |
346 | non-overlapping instances of the regular | |
347 | expression rather than just | |
348 | the first one. If both | |
349 | .Cm g | |
350 | and | |
351 | .Cm n | |
352 | are given, | |
353 | .Cm g | |
354 | takes precedence. | |
355 | .It Cm p | |
356 | Prints the pattern space if a | |
357 | replacement was made. | |
358 | .It Cm w Ar wfile | |
359 | Write. Appends the pattern space | |
360 | to | |
361 | .Ar wfile | |
362 | if a replacement was | |
363 | made. | |
364 | .El | |
365 | .It Oo Ad 2addr Oc Ns Cm t Ar label | |
2d33ba58 | 366 | Test. |
7701b0dd CL |
367 | Branches to the |
368 | .Cm \&: | |
369 | command bearing the | |
370 | label if any substitutions have been made | |
371 | since the most recent reading of an input | |
372 | line or execution of a | |
373 | .Cm t . | |
374 | If label is empty, | |
375 | branches to the end of the script. | |
376 | .It Oo Ad 2addr Oc Ns Cm w Ar wfile | |
377 | Appends | |
378 | .Op writes | |
379 | the pattern space to | |
380 | .Ar wfile . | |
381 | .It Oo Ad 2addr Oc Ns Cm x | |
382 | Exchanges the contents of the pattern and | |
383 | hold spaces. | |
384 | .It Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/ | |
385 | Replaces all occurrences of collating | |
386 | elements in | |
387 | .Ar string1 | |
388 | with the corresponding | |
389 | collating element in | |
390 | .Ar string2 . | |
2d33ba58 | 391 | The lengths of |
7701b0dd | 392 | .Ar string1 |
2d33ba58 | 393 | and |
7701b0dd CL |
394 | .Ar string2 |
395 | shall be equal. | |
396 | .It Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction | |
397 | Applies the function (or group, if function | |
398 | is {) only to the lines that are not selected | |
399 | by the address(es). | |
400 | .It Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel | |
401 | This command does nothing; it bears a label | |
402 | for the b and t commands to branch to. | |
403 | .It Oo Ad 1addr Oc Ns Cm \&= | |
404 | Places the current line number on the standard | |
405 | output as a line with its own line | |
406 | number. | |
407 | .It Oo Ad 0addr Oc | |
2d33ba58 | 408 | An empty command is ignored. |
7701b0dd CL |
409 | .It Oo Ad 0addr Oc Cm # |
410 | If a | |
411 | .Cm # | |
412 | appears as the first character on any | |
413 | line of a script file, that entire line is | |
414 | ignored (treated as a comment), with the single | |
415 | exception that if the first line of the | |
416 | script file begins with | |
417 | .Cm Ns #n , | |
418 | the default | |
419 | output is suppressed. | |
420 | .El | |
421 | .Pp | |
422 | The | |
423 | .Nm sed | |
424 | utility exits 0 on success, and >0 if an error occurs. | |
425 | .Pp | |
426 | If one or more of the input (not script) files cannot be | |
427 | opened for reading, | |
428 | .Nm sed | |
429 | continues to process the remaining | |
430 | files. | |
431 | .Sh STANDARDS | |
432 | The | |
433 | .Nm sed | |
434 | utility is expected to be | |
435 | .St -p1003.2 | |
436 | compatible. |