1sed(1) User Commands sed(1)
2
3
4
6 sed - stream editor
7
9 /usr/bin/sed [-n] script [file]...
10
11
12 /usr/bin/sed [-n] [-e script]... [-f script_file]...
13 [file]...
14
15
16 /usr/xpg4/bin/sed [-n] script [file]...
17
18
19 /usr/xpg4/bin/sed [-n] [-e script]... [-f script_file]...
20 [file]...
21
22
24 The sed utility is a stream editor that reads one or more text files,
25 makes editing changes according to a script of editing commands, and
26 writes the results to standard output. The script is obtained from
27 either the script operand string, or a combination of the option-argu‐
28 ments from the -e script and -f script_file options.
29
30
31 The sed utility is a text editor. It cannot edit binary files or files
32 containing ASCII NUL (\0) characters or very long lines.
33
35 The following options are supported:
36
37 -e script script is an edit command for sed. See USAGE below
38 for more information on the format of script. If
39 there is just one -e option and no -f options, the
40 flag -e may be omitted.
41
42
43 -f script_file Takes the script from script_file. script_file con‐
44 sists of editing commands, one per line.
45
46
47 -n Suppresses the default output.
48
49
50
51 Multiple -e and -f options may be specified. All commands are added to
52 the script in the order specified, regardless of their origin.
53
55 The following operands are supported:
56
57 file A path name of a file whose contents will be read and
58 edited. If multiple file operands are specified, the named
59 files will be read in the order specified and the concatena‐
60 tion will be edited. If no file operands are specified, the
61 standard input will be used.
62
63
64 script A string to be used as the script of editing commands. The
65 application must not present a script that violates the
66 restrictions of a text file except that the final character
67 need not be a NEWLINE character.
68
69
71 A script consists of editing commands, one per line, of the following
72 form:
73
74
75 [ address [ , address ] ] command [ arguments ]
76
77
78 Zero or more blank characters are accepted before the first address and
79 before command. Any number of semicolons are accepted before the first
80 address.
81
82
83 In normal operation, sed cyclically copies a line of input (less its
84 terminating NEWLINE character) into a pattern space (unless there is
85 something left after a D command), applies in sequence all commands
86 whose addresses select that pattern space, and copies the resulting
87 pattern space to the standard output (except under -n) and deletes the
88 pattern space. Whenever the pattern space is written to standard output
89 or a named file, sed will immediately follow it with a NEWLINE charac‐
90 ter.
91
92
93 Some of the commands use a hold space to save all or part of the pat‐
94 tern space for subsequent retrieval. The pattern and hold spaces will
95 each be able to hold at least 8192 bytes.
96
97 sed Addresses
98 An address is either empty, a decimal number that counts input lines
99 cumulatively across files, a $ that addresses the last line of input,
100 or a context address, which consists of a /regular expression/ as
101 described on the regexp(5) manual page.
102
103
104 A command line with no addresses selects every pattern space.
105
106
107 A command line with one address selects each pattern space that matches
108 the address.
109
110
111 A command line with two addresses selects the inclusive range from the
112 first pattern space that matches the first address through the next
113 pattern space that matches the second address. Thereafter the process
114 is repeated, looking again for the first address. (If the second
115 address is a number less than or equal to the line number selected by
116 the first address, only the line corresponding to the first address is
117 selected.)
118
119
120 Typically, address are separated from each other by a comma (,). They
121 may also be separated by a semicolon (;).
122
123 sed Regular Expressions
124 sed supports the basic regular expressions described on the regexp(5)
125 manual page, with the following additions:
126
127 \cREc In a context address, the construction \cREc, where c is any
128 character other than a backslash or NEWLINE character, is
129 identical to /RE/. If the character designated by c appears
130 following a backslash, then it is considered to be that lit‐
131 eral character, which does not terminate the RE. For exam‐
132 ple, in the context address \xabc\xdefx, the second x stands
133 for itself, so that the regular expression is abcxdef.
134
135
136 \n The escape sequence \n matches a NEWLINE character embedded
137 in the pattern space. A literal NEWLINE character must not
138 be used in the regular expression of a context address or in
139 the substitute command.
140
141
142
143 Editing commands can be applied only to non-selected pattern spaces by
144 use of the negation command ! (described below).
145
146 sed Editing Commands
147 In the following list of functions the maximum number of permissible
148 addresses for each function is indicated.
149
150
151 The r and w commands take an optional rfile (or wfile) parameter, sepa‐
152 rated from the command letter by one or more blank characters.
153
154
155 Multiple commands can be specified by separating them with a semicolon
156 (;) on the same command line.
157
158
159 The text argument consists of one or more lines, all but the last of
160 which end with \ to hide the NEWLINE. Each embedded NEWLINE character
161 in the text must be preceded by a backslash. Other backslashes in text
162 are removed and the following character is treated literally. Back‐
163 slashes in text are treated like backslashes in the replacement string
164 of an s command, and may be used to protect initial blanks and tabs
165 against the stripping that is done on every script line. The rfile or
166 wfile argument must terminate the command line and must be preceded by
167 exactly one blank. The use of the wfile parameter causes that file to
168 be initially created, if it does not exist, or will replace the con‐
169 tents of an existing file. There can be at most 10 distinct wfile argu‐
170 ments.
171
172
173 Regular expressions match entire strings, not just individual lines,
174 but a NEWLINE character is matched by \n in a sed RE. A NEWLINE charac‐
175 ter is not allowed in an RE. Also notice that \n cannot be used to
176 match a NEWLINE character at the end of an input line; NEWLINE charac‐
177 ters appear in the pattern space as a result of the N editing command.
178
179
180 Two of the commands take a command-list, which is a list of sed com‐
181 mands separated by NEWLINE characters, as follows:
182
183 { command
184 command
185 }
186
187
188
189 The { can be preceded with blank characters and can be followed with
190 white space. The commands can be preceded by white space. The terminat‐
191 ing } must be preceded by a NEWLINE character and can be preceded or
192 followed by <blank>s. The braces may be preceded or followed by
193 <blank>s. The command may be preceded by <blank>s, but may not be fol‐
194 lowed by <blank>s.
195
196
197 The following table lists the functions, with the maximum number of
198 permissible addresses.
199
200
201
202
203 ┌────────────┬────────────────┬──────────────────────────────────┐
204 │Max Address │ Command │ Description │
205 ├────────────┼────────────────┼──────────────────────────────────┤
206 │1 │ a\ text │ Append by executing N command or │
207 │ │ │ beginning a new cycle. Place │
208 │ │ │ text on the output before read‐ │
209 │ │ │ ing the next input line. │
210 ├────────────┼────────────────┼──────────────────────────────────┤
211 │2 │ b label │ Branch to the : command bearing │
212 │ │ │ the label . If label is empty, │
213 │ │ │ branch to the end of the script. │
214 │ │ │ Labels are recognized unique up │
215 │ │ │ to eight characters. │
216 ├────────────┼────────────────┼──────────────────────────────────┤
217 │2 │ c\ text │ Change. Delete the pattern │
218 │ │ │ space. Place text on the out‐ │
219 │ │ │ put. Start the next cycle. │
220 │2 │ d │ Delete the pattern space. Start │
221 │ │ │ the next cycle. │
222 ├────────────┼────────────────┼──────────────────────────────────┤
223 │2 │ D │ Delete the initial segment of │
224 │ │ │ the pattern space through the │
225 │ │ │ first new-line. Start the next │
226 │ │ │ cycle. (See the N command │
227 │ │ │ below.) │
228 ├────────────┼────────────────┼──────────────────────────────────┤
229 │2 │ g │ Replace the contents of the pat‐ │
230 │ │ │ tern space by the contents of │
231 │ │ │ the hold space. │
232 ├────────────┼────────────────┼──────────────────────────────────┤
233 │2 │ G │ Append the contents of the hold │
234 │ │ │ space to the pattern space. │
235 ├────────────┼────────────────┼──────────────────────────────────┤
236 │2 │ h │ Replace the contents of the hold │
237 │ │ │ space by the contents of the │
238 │ │ │ pattern space. │
239 ├────────────┼────────────────┼──────────────────────────────────┤
240 │2 │ H │ Append the contents of the pat‐ │
241 │ │ │ tern space to the hold space. │
242 ├────────────┼────────────────┼──────────────────────────────────┤
243 │1 │ i\ text │ Insert. Place text on the stan‐ │
244 │ │ │ dard output. │
245 ├────────────┼────────────────┼──────────────────────────────────┤
246 │2 │ l │ /usr/bin/sed: List the pattern │
247 │ │ │ space on the standard output in │
248 │ │ │ an unambiguous form. Non-print‐ │
249 │ │ │ able characters are displayed in │
250 │ │ │ octal notation and long lines │
251 │ │ │ are folded. │
252 ├────────────┼────────────────┼──────────────────────────────────┤
253 │ │ │ /usr/xpg4/bin/sed: List the │
254 │ │ │ pattern space on the standard │
255 │ │ │ output in an unambiguous form. │
256 │ │ │ Non-printable characters are │
257 │ │ │ displayed in octal notation and │
258 │ │ │ long lines are folded. The char‐ │
259 │ │ │ acters (\\, \a, \b, \f, \r, \t, │
260 │ │ │ and \v) are written as the cor‐ │
261 │ │ │ responding escape sequences. │
262 │ │ │ Non-printable characters not in │
263 │ │ │ that table will be written as │
264 │ │ │ one three-digit octal number │
265 │ │ │ (with a preceding backslash │
266 │ │ │ character) for each byte in the │
267 │ │ │ character (most significant byte │
268 │ │ │ first). If the size of a byte on │
269 │ │ │ the system is greater than nine │
270 │ │ │ bits, the format used for non- │
271 │ │ │ printable characters is imple‐ │
272 │ │ │ mentation dependent. │
273 │ │ │ Long lines are folded, with the │
274 │ │ │ point of folding indicated by │
275 │ │ │ writing a backslash followed by │
276 │ │ │ a NEWLINE; the length at which │
277 │ │ │ folding occurs is unspecified, │
278 │ │ │ but should be appropriate for │
279 │ │ │ the output device. The end of │
280 │ │ │ each line is marked with a $. │
281 ├────────────┼────────────────┼──────────────────────────────────┤
282 │2 │ n │ Copy the pattern space to the │
283 │ │ │ standard output if default out‐ │
284 │ │ │ put is not suppressed. Replace │
285 │ │ │ the pattern space with the next │
286 │ │ │ line of input. │
287 ├────────────┼────────────────┼──────────────────────────────────┤
288 │2 │ N │ Append the next line of input to │
289 │ │ │ the pattern space with an embed‐ │
290 │ │ │ ded new-line. (The current line │
291 │ │ │ number changes.) If no next │
292 │ │ │ line of input is available, the │
293 │ │ │ N command verb shall branch to │
294 │ │ │ the end of the script and quit │
295 │ │ │ without starting a new cycle and │
296 │ │ │ without writing the pattern │
297 │ │ │ space. │
298 ├────────────┼────────────────┼──────────────────────────────────┤
299 │2 │ p │ Print. Copy the pattern space │
300 │ │ │ to the standard output. │
301 ├────────────┼────────────────┼──────────────────────────────────┤
302 │2 │ P │ Copy the initial segment of the │
303 │ │ │ pattern space through the first │
304 │ │ │ new-line to the standard output. │
305 ├────────────┼────────────────┼──────────────────────────────────┤
306 │1 │ q │ Quit. Branch to the end of the │
307 │ │ │ script. Do not start a new │
308 │ │ │ cycle. │
309 ├────────────┼────────────────┼──────────────────────────────────┤
310 │2 │ r rfile │ Read the contents of rfile. │
311 │ │ │ Place them on the output before │
312 │ │ │ reading the next input line. If │
313 │ │ │ rfile does not exist or cannot │
314 │ │ │ be read, it is treated as if it │
315 │ │ │ were an empty file, causing no │
316 │ │ │ error condition. │
317 ├────────────┼────────────────┼──────────────────────────────────┤
318 │2 │ t label │ Test. Branch to the : command │
319 │ │ │ bearing the label if any substi‐ │
320 │ │ │ tutions have been made since the │
321 │ │ │ most recent reading of an input │
322 │ │ │ line or execution of a t. If │
323 │ │ │ label is empty, branch to the │
324 │ │ │ end of the script. │
325 ├────────────┼────────────────┼──────────────────────────────────┤
326 │2 │ w wfile │ Write. Append the pattern space │
327 │ │ │ to wfile. The first occurrence │
328 │ │ │ of w will cause wfile to be │
329 │ │ │ cleared. Subsequent invocations │
330 │ │ │ of w will append. Each time the │
331 │ │ │ sed command is used, wfile is │
332 │ │ │ overwritten. │
333 ├────────────┼────────────────┼──────────────────────────────────┤
334 │2 │ x │ Exchange the contents of the │
335 │ │ │ pattern and hold spaces. │
336 ├────────────┼────────────────┼──────────────────────────────────┤
337 │2 │ ! command │ Don't. Apply the command (or │
338 │ │ │ group, if command is {) only to │
339 │ │ │ lines not selected by the │
340 │ │ │ address(es). │
341 ├────────────┼────────────────┼──────────────────────────────────┤
342 │0 │ : label │ This command does nothing; it │
343 │ │ │ bears a label for b and t com‐ │
344 │ │ │ mands to branch to. │
345 ├────────────┼────────────────┼──────────────────────────────────┤
346 │1 │ = │ Place the current line number on │
347 │ │ │ the standard output as a line. │
348 ├────────────┼────────────────┼──────────────────────────────────┤
349 │2 │ {command-list} │ Execute command-list only when │
350 │ │ │ the pattern space is selected. │
351 │0 │ │ An empty command is ignored. │
352 ├────────────┼────────────────┼──────────────────────────────────┤
353 │0 │ # │ If a # appears as the first │
354 │ │ │ character on a line of a script │
355 │ │ │ file, then that entire line is │
356 │ │ │ treated as a comment, with one │
357 │ │ │ exception: if a # appears on the │
358 │ │ │ first line and the character │
359 │ │ │ after the # is an n, then the │
360 │ │ │ default output will be sup‐ │
361 │ │ │ pressed. The rest of the line │
362 │ │ │ after #n is also ignored. A │
363 │ │ │ script file must contain at │
364 │ │ │ least one non-comment line. │
365 └────────────┴────────────────┴──────────────────────────────────┘
366
367
368
369
370 ┌───────────┬───────────────────────────────────────────────┐
371 │Max Addr │ Command (Using strings) and Description │
372 ├───────────┼───────────────────────────────────────────────┤
373 │2 │s/regular expression/replacement/flags │
374 │ │Substitute the replacement string for │
375 │ │instances of the regular expression in the │
376 │ │pattern space. Any character other than back‐ │
377 │ │slash or newline can be used instead of a │
378 │ │slash to delimit the RE and the replacement. │
379 │ │Within the RE and the replacement, the RE │
380 │ │delimiter itself can be used as a literal │
381 │ │character if it is preceded by a backslash. │
382 │ │An ampersand (&) appearing in the replacement │
383 │ │will be replaced by the string matching the │
384 │ │RE. The special meaning of & in this context │
385 │ │can be suppressed by preceding it by back‐ │
386 │ │slash. The characters \n, where n is a digit, │
387 │ │will be replaced by the text matched by the │
388 │ │corresponding backreference expression. For │
389 │ │each backslash (\) encountered in scanning │
390 │ │replacement from beginning to end, the follow‐ │
391 │ │ing character loses its special meaning (if │
392 │ │any). It is unspecified what special meaning │
393 │ │is given to any character other than &, \ or │
394 │ │digits. │
395 │ │A line can be split by substituting a NEWLINE │
396 │ │character into it. The application must │
397 │ │escape the NEWLINE character in the replace‐ │
398 │ │ment by preceding it with backslash. A sub‐ │
399 │ │stitution is considered to have been performed │
400 │ │even if the replacement string is identical to │
401 │ │the string that it replaces. │
402 │ │flags is zero or more of: │
403 │ │n n= 1 - 512. Substitute for just the nth │
404 │ │occurrence of the regular expression. │
405 │ │g Global. Substitute for all nonoverlapping │
406 │ │instances of the regular expression rather │
407 │ │than just the first one. If both g and n are │
408 │ │specified, the results are unspecified. │
409 ├───────────┼───────────────────────────────────────────────┤
410 │ │p Print the pattern space if a replacement was │
411 │ │made. │
412 │ │P Copy the initial segment of the pattern │
413 │ │space through the first new-line to the stan‐ │
414 │ │dard output. │
415 │ │w wfile Write. Append the pattern space to │
416 │ │wfile if a replacement was made. The first │
417 │ │occurrence of w will cause wfile to be │
418 │ │cleared. Subsequent invocations of w will │
419 │ │append. Each time the sed command is used, │
420 │ │wfile is overwritten. │
421 ├───────────┼───────────────────────────────────────────────┤
422 │2 │y/ string1 / string2 / │
423 │ │Transform. Replace all occurrences of charac‐ │
424 │ │ters in string1 with the corresponding char‐ │
425 │ │acters in string2. string1 and string2 must │
426 │ │have the same number of characters, or if any │
427 │ │of the characters in string1 appear more than │
428 │ │once, the results are undefined. Any charac‐ │
429 │ │ter other than backslash or NEWLINE can be │
430 │ │used instead of slash to delimit the strings. │
431 │ │Within string1 and string2, the delimiter │
432 │ │itself can be used as a literal character if │
433 │ │it is preceded by a backslash. For example, │
434 │ │y/abc/ABC/ replaces a with A, b with B, and c │
435 │ │with C. │
436 └───────────┴───────────────────────────────────────────────┘
437
438
439 See largefile(5) for the description of the behavior of sed when
440 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
441
443 Example 1 An example sed script
444
445
446 This sed script simulates the BSD cat -s command, squeezing excess
447 blank lines from standard input.
448
449
450 sed −n '
451 # Write non-empty lines.
452 /./ {
453 p
454 d
455 }
456 # Write a single empty line, then look for more empty lines.
457 /^$/ p
458 # Get next line, discard the held <newline> (empty line),
459 # and look for more empty lines.
460 :Empty
461 /^$/ {
462 N
463 s/.//
464 b Empty
465 }
466 # Write the non-empty line before going back to search
467 # for the first in a set of empty lines.
468 p
469 '
470
471
473 See environ(5) for descriptions of the following environment variables
474 that affect the execution of sed: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
475 LC_MESSAGES, and NLSPATH.
476
478 The following exit values are returned:
479
480 0 Successful completion.
481
482
483 >0 An error occurred.
484
485
487 See attributes(5) for descriptions of the following attributes:
488
489 /usr/bin/sed
490 ┌─────────────────────────────┬─────────────────────────────┐
491 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
492 ├─────────────────────────────┼─────────────────────────────┤
493 │Availability │SUNWcsu │
494 ├─────────────────────────────┼─────────────────────────────┤
495 │CSI │Not enabled │
496 └─────────────────────────────┴─────────────────────────────┘
497
498 /usr/xpg4/bin/sed
499 ┌─────────────────────────────┬─────────────────────────────┐
500 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
501 ├─────────────────────────────┼─────────────────────────────┤
502 │Availability │SUNWxcu4 │
503 ├─────────────────────────────┼─────────────────────────────┤
504 │CSI │Enabled │
505 ├─────────────────────────────┼─────────────────────────────┤
506 │Interface Stability │Standard │
507 └─────────────────────────────┴─────────────────────────────┘
508
510 awk(1), ed(1), grep(1), attributes(5), environ(5), largefile(5), reg‐
511 exp(5), standards(5)
512
513
514
515SunOS 5.11 23 Jul 1998 sed(1)