logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

sed.1p (30706B)


  1. '\" et
  2. .TH SED "1P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. sed
  12. \(em stream editor
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. sed \fB[\fR-n\fB] \fIscript \fB[\fIfile\fR...\fB]\fR
  17. .P
  18. sed \fB[\fR-n\fB] \fR-e \fIscript \fB[\fR-e \fIscript\fB]\fR... \fB[\fR-f \fIscript_file\fB]\fR... \fB[\fIfile\fR...\fB]\fR
  19. .P
  20. sed \fB[\fR-n\fB] [\fR-e \fIscript\fB]\fR... -f \fIscript_file\fR \fB[\fR-f \fIscript_file\fB]\fR... \fB[\fIfile\fR...\fB]\fR
  21. .fi
  22. .SH DESCRIPTION
  23. The
  24. .IR sed
  25. utility is a stream editor that shall read one or more text files, make
  26. editing changes according to a script of editing commands, and write
  27. the results to standard output. The script shall be obtained from
  28. either the
  29. .IR script
  30. operand string or a combination of the option-arguments from the
  31. .BR \-e
  32. .IR script
  33. and
  34. .BR \-f
  35. .IR script_file
  36. options.
  37. .SH OPTIONS
  38. The
  39. .IR sed
  40. utility shall conform to the Base Definitions volume of POSIX.1\(hy2017,
  41. .IR "Section 12.2" ", " "Utility Syntax Guidelines",
  42. except that the order of presentation of the
  43. .BR \-e
  44. and
  45. .BR \-f
  46. options is significant.
  47. .P
  48. The following options shall be supported:
  49. .IP "\fB\-e\ \fIscript\fR" 10
  50. Add the editing commands specified by the
  51. .IR script
  52. option-argument to the end of the script of editing commands.
  53. .IP "\fB\-f\ \fIscript_file\fR" 10
  54. Add the editing commands in the file
  55. .IR script_file
  56. to the end of the script of editing commands.
  57. .IP "\fB\-n\fP" 10
  58. Suppress the default output (in which each line, after it is examined
  59. for editing, is written to standard output). Only lines explicitly
  60. selected for output are written.
  61. .P
  62. If any
  63. .BR \-e
  64. or
  65. .BR \-f
  66. options are specified, the script of editing commands shall initially
  67. be empty. The commands specified by each
  68. .BR \-e
  69. or
  70. .BR \-f
  71. option shall be added to the script in the order specified. When each
  72. addition is made, if the previous addition (if any) was from a
  73. .BR \-e
  74. option, a
  75. <newline>
  76. shall be inserted before the new addition. The resulting script shall
  77. have the same properties as the
  78. .IR script
  79. operand, described in the OPERANDS section.
  80. .SH OPERANDS
  81. The following operands shall be supported:
  82. .IP "\fIfile\fR" 10
  83. A pathname of a file whose contents are read and edited. If multiple
  84. .IR file
  85. operands are specified, the named files shall be read in the order
  86. specified and the concatenation shall be edited. If no
  87. .IR file
  88. operands are specified, the standard input shall be used.
  89. .IP "\fIscript\fR" 10
  90. A string to be used as the script of editing commands. The application
  91. shall not present a
  92. .IR script
  93. that violates the restrictions of a text file except that the final
  94. character need not be a
  95. <newline>.
  96. .SH STDIN
  97. The standard input shall be used if no
  98. .IR file
  99. operands are specified, and shall be used if a
  100. .IR file
  101. operand is
  102. .BR '\-'
  103. and the implementation treats the
  104. .BR '\-'
  105. as meaning standard input.
  106. Otherwise, the standard input shall not be used.
  107. See the INPUT FILES section.
  108. .SH "INPUT FILES"
  109. The input files shall be text files. The
  110. .IR script_file s
  111. named by the
  112. .BR \-f
  113. option shall consist of editing commands.
  114. .SH "ENVIRONMENT VARIABLES"
  115. The following environment variables shall affect the execution of
  116. .IR sed :
  117. .IP "\fILANG\fP" 10
  118. Provide a default value for the internationalization variables that are
  119. unset or null. (See the Base Definitions volume of POSIX.1\(hy2017,
  120. .IR "Section 8.2" ", " "Internationalization Variables"
  121. for the precedence of internationalization variables used to determine
  122. the values of locale categories.)
  123. .IP "\fILC_ALL\fP" 10
  124. If set to a non-empty string value, override the values of all the
  125. other internationalization variables.
  126. .IP "\fILC_COLLATE\fP" 10
  127. .br
  128. Determine the locale for the behavior of ranges, equivalence classes,
  129. and multi-character collating elements within regular expressions.
  130. .IP "\fILC_CTYPE\fP" 10
  131. Determine the locale for the interpretation of sequences of bytes of
  132. text data as characters (for example, single-byte as opposed to
  133. multi-byte characters in arguments and input files), and the behavior
  134. of character classes within regular expressions.
  135. .IP "\fILC_MESSAGES\fP" 10
  136. .br
  137. Determine the locale that should be used to affect the format and
  138. contents of diagnostic messages written to standard error.
  139. .IP "\fINLSPATH\fP" 10
  140. Determine the location of message catalogs for the processing of
  141. .IR LC_MESSAGES .
  142. .SH "ASYNCHRONOUS EVENTS"
  143. Default.
  144. .SH STDOUT
  145. The input files shall be written to standard output, with the editing
  146. commands specified in the script applied. If the
  147. .BR \-n
  148. option is specified, only those input lines selected by the script
  149. shall be written to standard output.
  150. .SH STDERR
  151. The standard error shall be used only for diagnostic and warning messages.
  152. .SH "OUTPUT FILES"
  153. The output files shall be text files whose formats are dependent on the
  154. editing commands given.
  155. .SH "EXTENDED DESCRIPTION"
  156. The
  157. .IR script
  158. shall consist of editing commands of the following form:
  159. .sp
  160. .RS 4
  161. .nf
  162. \fB[\fIaddress\fB[\fR,\fIaddress\fB]]\fIfunction\fR
  163. .fi
  164. .P
  165. .RE
  166. .P
  167. where
  168. .IR function
  169. represents a single-character command verb from the list in
  170. .IR "Editing Commands in sed",
  171. followed by any applicable arguments.
  172. .P
  173. The command can be preceded by
  174. <blank>
  175. characters and/or
  176. <semicolon>
  177. characters. The function can be preceded by
  178. <blank>
  179. characters. These optional characters shall have no effect.
  180. .P
  181. In default operation,
  182. .IR sed
  183. cyclically shall append a line of input, less its terminating
  184. <newline>
  185. character, into the pattern space. Reading from input shall be skipped
  186. if a
  187. <newline>
  188. was in the pattern space prior to a
  189. .BR D
  190. command ending the previous cycle. The
  191. .IR sed
  192. utility shall then apply in sequence all commands whose addresses select
  193. that pattern space, until a command starts the next cycle or quits. If
  194. no commands explicitly started a new cycle, then at the end of the script
  195. the pattern space shall be copied to standard output (except when
  196. .BR \-n
  197. is specified) and the pattern space shall be deleted. Whenever the
  198. pattern space is written to standard output or a named file,
  199. .IR sed
  200. shall immediately follow it with a
  201. <newline>.
  202. .P
  203. Some of the editing commands use a hold space to save all or part of
  204. the pattern space for subsequent retrieval. The pattern and hold spaces
  205. shall each be able to hold at least 8\|192 bytes.
  206. .SS "Addresses in sed"
  207. .P
  208. An address is either a decimal number that counts input lines
  209. cumulatively across files, a
  210. .BR '$'
  211. character that addresses the last line of input, or a context address
  212. (which consists of a BRE, as described in
  213. .IR "Regular Expressions in sed",
  214. preceded and followed by a delimiter, usually a
  215. <slash>).
  216. .P
  217. An editing command with no addresses shall select every pattern space.
  218. .P
  219. An editing command with one address shall select each pattern space
  220. that matches the address.
  221. .P
  222. An editing command with two addresses shall select the inclusive range
  223. from the first pattern space that matches the first address through the
  224. next pattern space that matches the second. (If the second address is a
  225. number less than or equal to the line number first selected, only one
  226. line shall be selected.) Starting at the first line following the
  227. selected range,
  228. .IR sed
  229. shall look again for the first address. Thereafter, the process shall
  230. be repeated. Omitting either or both of the address components in the
  231. following form produces undefined results:
  232. .sp
  233. .RS 4
  234. .nf
  235. \fB[\fIaddress\fB[\fR,\fIaddress\fB]]\fR
  236. .fi
  237. .P
  238. .RE
  239. .SS "Regular Expressions in sed"
  240. .P
  241. The
  242. .IR sed
  243. utility shall support the BREs described in the Base Definitions volume of POSIX.1\(hy2017,
  244. .IR "Section 9.3" ", " "Basic Regular Expressions",
  245. with the following additions:
  246. .IP " *" 4
  247. In a context address, the construction
  248. .BR \(dq\ecBREc\(dq ,
  249. where
  250. .IR c
  251. is any character other than
  252. <backslash>
  253. or
  254. <newline>,
  255. shall be identical to
  256. .BR \(dq/BRE/\(dq .
  257. If the character designated by
  258. .IR c
  259. appears following a
  260. <backslash>,
  261. then it shall be considered to be that literal character, which shall
  262. not terminate the BRE. For example, in the context address
  263. .BR \(dq\exabc\exdefx\(dq ,
  264. the second
  265. .IR x
  266. stands for itself, so that the BRE is
  267. .BR \(dqabcxdef\(dq .
  268. .IP " *" 4
  269. The escape sequence
  270. .BR '\en'
  271. shall match a
  272. <newline>
  273. embedded in the pattern space. A literal
  274. <newline>
  275. shall not be used in the BRE of a context address or in the substitute
  276. function.
  277. .IP " *" 4
  278. If an RE is empty (that is, no pattern is specified)
  279. .IR sed
  280. shall behave as if the last RE used in the last command applied (either
  281. as an address or as part of a substitute command) was specified.
  282. .SS "Editing Commands in sed"
  283. .P
  284. In the following list of editing commands, the maximum number of
  285. permissible addresses for each function is indicated by [\c
  286. .IR 0addr ],
  287. [\c
  288. .IR 1addr ],
  289. or [\c
  290. .IR 2addr ],
  291. representing zero, one, or two addresses.
  292. .P
  293. The argument
  294. .IR text
  295. shall consist of one or more lines. Each embedded
  296. <newline>
  297. in the text shall be preceded by a
  298. <backslash>.
  299. Other
  300. <backslash>
  301. characters in text shall be removed, and the following character shall
  302. be treated literally.
  303. .P
  304. The
  305. .BR r
  306. and
  307. .BR w
  308. command verbs, and the
  309. .IR w
  310. flag to the
  311. .BR s
  312. command, take an
  313. .IR rfile
  314. (or
  315. .IR wfile )
  316. parameter, separated from the command verb letter or flag by one or
  317. more
  318. <blank>
  319. characters; implementations may allow zero separation as an extension.
  320. .P
  321. The argument
  322. .IR rfile
  323. or the argument
  324. .IR wfile
  325. shall terminate the editing command. Each
  326. .IR wfile
  327. shall be created before processing begins. Implementations shall
  328. support at least ten
  329. .IR wfile
  330. arguments in the script; the actual number (greater than or equal to
  331. 10) that is supported by the implementation is unspecified. The
  332. use of the
  333. .IR wfile
  334. parameter shall cause that file to be initially created, if it does not
  335. exist, or shall replace the contents of an existing file.
  336. .P
  337. The
  338. .BR b ,
  339. .BR r ,
  340. .BR s ,
  341. .BR t ,
  342. .BR w ,
  343. .BR y ,
  344. and
  345. .BR :
  346. command verbs shall accept additional arguments. The following synopses
  347. indicate which arguments shall be separated from the command verbs by a
  348. single
  349. <space>.
  350. .P
  351. The
  352. .BR a
  353. and
  354. .BR r
  355. commands schedule text for later output. The text specified for the
  356. .BR a
  357. command, and the contents of the file specified for the
  358. .BR r
  359. command, shall be written to standard output just before the next
  360. attempt to fetch a line of input when executing the
  361. .BR N
  362. or
  363. .BR n
  364. commands, or when reaching the end of the script. If written when
  365. reaching the end of the script, and the
  366. .BR \-n
  367. option was not specified, the text shall be written after copying the
  368. pattern space to standard output. The contents of the file specified
  369. for the
  370. .BR r
  371. command shall be as of the time the output is written, not the time the
  372. .BR r
  373. command is applied. The text shall be output in the order in which the
  374. .BR a
  375. and
  376. .BR r
  377. commands were applied to the input.
  378. .P
  379. Editing commands other than
  380. .BR {...} ,
  381. .BR a ,
  382. .BR b ,
  383. .BR c ,
  384. .BR i ,
  385. .BR r ,
  386. .BR t ,
  387. .BR w ,
  388. .BR : ,
  389. and
  390. .BR #
  391. can be followed by a
  392. <semicolon>,
  393. optional
  394. <blank>
  395. characters, and another editing command. However, when an
  396. .BR s
  397. editing command is used with the
  398. .IR w
  399. flag, following it with another command in this manner produces
  400. undefined results.
  401. .P
  402. A function can be preceded by a
  403. .BR '!'
  404. character, in which case the function shall be applied if the
  405. addresses do not select the pattern space. Zero or more
  406. <blank>
  407. characters shall be accepted before the
  408. .BR '!'
  409. character. It is unspecified whether
  410. <blank>
  411. characters can follow the
  412. .BR '!'
  413. character, and conforming applications shall not follow the
  414. .BR '!'
  415. character with
  416. <blank>
  417. characters.
  418. .P
  419. If a
  420. .IR label
  421. argument (to a
  422. .BR b ,
  423. .BR t ,
  424. or
  425. .BR :
  426. command) contains characters outside of the portable filename
  427. character set, or if a
  428. .IR label
  429. is longer than 8 bytes, the behavior is unspecified. The
  430. implementation shall support
  431. .IR label
  432. arguments recognized as unique up to at least 8 bytes; the actual
  433. length (greater than or equal to 8) supported by the implementation
  434. is unspecified. It is unspecified whether exceeding the maximum
  435. supported label length causes an error or a silent truncation.
  436. .IP "\fB[\fI2addr\fB]\ {\fIediting command\fR" 10
  437. .IP "\fIediting command\fR" 10
  438. .IP ".\|.\|." 10
  439. .IP "\fB}\fR" 10
  440. Execute a list of
  441. .IR sed
  442. editing commands only when the pattern space is selected. The list of
  443. .IR sed
  444. editing commands shall be surrounded by braces. The braces can be
  445. preceded or followed by
  446. <blank>
  447. characters. The
  448. <right-brace>
  449. shall be preceded by a
  450. <newline>
  451. or
  452. <semicolon>
  453. (before any optional
  454. <blank>
  455. characters preceding the
  456. <right-brace>).
  457. .RS 10
  458. .P
  459. Each command in the list of commands shall be terminated by a
  460. <newline>
  461. character, or by a
  462. <semicolon>
  463. character if permitted when the command is used outside the braces.
  464. The editing commands can be preceded by
  465. <blank>
  466. characters, but shall not be followed by
  467. <blank>
  468. characters.
  469. .RE
  470. .IP "\fB[\fI1addr\fB]a\e\fR" 10
  471. .IP "\fItext\fR" 10
  472. Write text to standard output as described previously.
  473. .IP "\fB[\fI2addr\fB]b\ [\fIlabel\fB]\fR" 10
  474. .br
  475. Branch to the
  476. .BR :
  477. command verb bearing the
  478. .IR label
  479. argument.
  480. If
  481. .IR label
  482. is not specified, branch to the end of the script.
  483. .IP "\fB[\fI2addr\fB]c\e\fR" 10
  484. .IP "\fItext\fR" 10
  485. Delete the pattern space. With a 0 or 1 address or at the end of a
  486. 2-address range, place
  487. .IR text
  488. on the output and start the next cycle.
  489. .IP "\fB[\fI2addr\fB]d\fR" 10
  490. Delete the pattern space and start the next cycle.
  491. .IP "\fB[\fI2addr\fB]D\fR" 10
  492. If the pattern space contains no
  493. <newline>,
  494. delete the pattern space and start a normal new cycle as if the
  495. .BR d
  496. command was issued. Otherwise, delete the initial segment of the
  497. pattern space through the first
  498. <newline>,
  499. and start the next cycle with the resultant pattern space and without
  500. reading any new input.
  501. .IP "\fB[\fI2addr\fB]g\fR" 10
  502. Replace the contents of the pattern space by the contents of the hold
  503. space.
  504. .IP "\fB[\fI2addr\fB]G\fR" 10
  505. Append to the pattern space a
  506. <newline>
  507. followed by the contents of the hold space.
  508. .IP "\fB[\fI2addr\fB]h\fR" 10
  509. Replace the contents of the hold space with the contents of the pattern
  510. space.
  511. .IP "\fB[\fI2addr\fB]H\fR" 10
  512. Append to the hold space a
  513. <newline>
  514. followed by the contents of the pattern space.
  515. .IP "\fB[\fI1addr\fB]i\e\fR" 10
  516. .IP "\fItext\fR" 10
  517. Write
  518. .IR text
  519. to standard output.
  520. .IP "\fB[\fI2addr\fB]l\fR" 10
  521. (The letter ell.) Write the pattern space to standard output in a
  522. visually unambiguous form. The characters listed in the Base Definitions volume of POSIX.1\(hy2017,
  523. .IR "Table 5-1" ", " "Escape Sequences and Associated Actions"
  524. (\c
  525. .BR '\e\e' ,
  526. .BR '\ea' ,
  527. .BR '\eb' ,
  528. .BR '\ef' ,
  529. .BR '\er' ,
  530. .BR '\et' ,
  531. .BR '\ev' )
  532. shall be written as the corresponding escape sequence; the
  533. .BR '\en'
  534. in that table is not applicable. Non-printable characters not in that
  535. table shall be written as one three-digit octal number (with a
  536. preceding
  537. <backslash>)
  538. for each byte in the character (most significant byte first).
  539. .RS 10
  540. .P
  541. Long lines shall be folded, with the point of folding indicated by
  542. writing a
  543. <backslash>
  544. followed by a
  545. <newline>;
  546. the length at which folding occurs is unspecified, but should be
  547. appropriate for the output device. The end of each line shall be marked
  548. with a
  549. .BR '$' .
  550. .RE
  551. .IP "\fB[\fI2addr\fB]n\fR" 10
  552. Write the pattern space to standard output if the default output has
  553. not been suppressed, and replace the pattern space with the next line
  554. of input, less its terminating
  555. <newline>.
  556. .RS 10
  557. .P
  558. If no next line of input is available, the
  559. .BR n
  560. command verb shall branch to the end of the script and quit without
  561. starting a new cycle.
  562. .RE
  563. .IP "\fB[\fI2addr\fB]N\fR" 10
  564. Append the next line of input, less its terminating
  565. <newline>,
  566. to the pattern space, using an embedded
  567. <newline>
  568. to separate the appended material from the original material. Note that
  569. the current line number changes.
  570. .RS 10
  571. .P
  572. If no next line of input is available, the
  573. .BR N
  574. command verb shall branch to the end of the script and quit without
  575. starting a new cycle or copying the pattern space to standard output.
  576. .RE
  577. .IP "\fB[\fI2addr\fB]p\fR" 10
  578. Write the pattern space to standard output.
  579. .IP "\fB[\fI2addr\fB]P\fR" 10
  580. Write the pattern space, up to the first
  581. <newline>,
  582. to standard output.
  583. .IP "\fB[\fI1addr\fB]q\fR" 10
  584. Branch to the end of the script and quit without starting a new cycle.
  585. .IP "\fB[\fI1addr\fB]r\ \fIrfile\fR" 10
  586. Copy the contents of
  587. .IR rfile
  588. to standard output as described previously. If
  589. .IR rfile
  590. does not exist or cannot be read, it shall be treated as if it were an
  591. empty file, causing no error condition.
  592. .IP "\fB[\fI2addr\fB]s/\fIBRE\fB/\fIreplacement\fB/\fIflags\fR" 10
  593. .br
  594. Substitute the replacement string for instances of the BRE in the
  595. pattern space. Any character other than
  596. <backslash>
  597. or
  598. <newline>
  599. can be used instead of a
  600. <slash>
  601. to delimit the BRE and the replacement. Within the BRE and the
  602. replacement, the BRE delimiter itself can be used as a literal character
  603. if it is preceded by a
  604. <backslash>.
  605. .RS 10
  606. .P
  607. The replacement string shall be scanned from beginning to end. An
  608. <ampersand>
  609. (\c
  610. .BR '&' )
  611. appearing in the replacement shall be replaced by the string matching
  612. the BRE. The special meaning of
  613. .BR '&'
  614. in this context can be suppressed by preceding it by a
  615. <backslash>.
  616. The characters \fR"\e\fIn"\fR, where
  617. .IR n
  618. is a digit, shall be replaced by the text matched by the corresponding
  619. back-reference expression. If the corresponding back-reference expression
  620. does not match, then the characters \fR"\e\fIn"\fR shall be replaced
  621. by the empty string. The special meaning of \fR"\e\fIn"\fR where
  622. .IR n
  623. is a digit in this context, can be suppressed by preceding it by a
  624. <backslash>.
  625. For each other
  626. <backslash>
  627. encountered, the following character shall lose its special meaning (if
  628. any).
  629. .P
  630. A line can be split by substituting a
  631. <newline>
  632. into it. The application shall escape the
  633. <newline>
  634. in the replacement by preceding it by a
  635. <backslash>.
  636. .P
  637. The meaning of an unescaped
  638. <backslash>
  639. immediately followed by any character other than
  640. .BR '&' ,
  641. <backslash>,
  642. a digit,
  643. <newline>,
  644. or the delimiter character used for this command, is unspecified.
  645. .P
  646. A substitution shall be considered to have been performed even if the
  647. replacement string is identical to the string that it replaces. Any
  648. <backslash>
  649. used to alter the default meaning of a subsequent character shall be
  650. discarded from the BRE or the replacement before evaluating the BRE or
  651. using the replacement.
  652. .P
  653. The value of
  654. .IR flags
  655. shall be zero or more of:
  656. .IP "\fIn\fR" 10
  657. Substitute for the
  658. .IR n th
  659. occurrence only of the BRE found within the pattern space.
  660. .IP "\fBg\fR" 10
  661. Globally substitute for all non-overlapping instances of the BRE rather
  662. than just the first one. If both
  663. .BR g
  664. and
  665. .IR n
  666. are specified, the results are unspecified.
  667. .IP "\fBp\fR" 10
  668. Write the pattern space to standard output if a replacement was made.
  669. .IP "\fBw\ \fIwfile\fR" 10
  670. Write. Append the pattern space to
  671. .IR wfile
  672. if a replacement was made. A conforming application shall precede the
  673. .IR wfile
  674. argument with one or more
  675. <blank>
  676. characters. If the
  677. .BR w
  678. flag is not the last flag value given in a concatenation of multiple
  679. flag values, the results are undefined.
  680. .RE
  681. .IP "\fB[\fI2addr\fB]t\ [\fIlabel\fB]\fR" 10
  682. .br
  683. Test. Branch to the
  684. .BR :
  685. command verb bearing the
  686. .IR label
  687. if any substitutions have been made since the most recent reading of an
  688. input line or execution of a
  689. .BR t .
  690. If
  691. .IR label
  692. is not specified, branch to the end of the script.
  693. .IP "\fB[\fI2addr\fB]w\ \fIwfile\fR" 10
  694. .br
  695. Append (write) the pattern space to
  696. .IR wfile .
  697. .IP "\fB[\fI2addr\fB]x\fR" 10
  698. Exchange the contents of the pattern and hold spaces.
  699. .IP "\fB[\fI2addr\fB]y/\fIstring1\fB/\fIstring2\fB/\fR" 10
  700. .br
  701. Replace all occurrences of characters in
  702. .IR string1
  703. with the corresponding characters in
  704. .IR string2 .
  705. If a
  706. <backslash>
  707. followed by an
  708. .BR 'n'
  709. appear in
  710. .IR string1
  711. or
  712. .IR string2 ,
  713. the two characters shall be handled as a single
  714. <newline>.
  715. If the number of characters in
  716. .IR string1
  717. and
  718. .IR string2
  719. are not equal, or if any of the characters in
  720. .IR string1
  721. appear more than once, the results are undefined. Any character other
  722. than
  723. <backslash>
  724. or
  725. <newline>
  726. can be used instead of
  727. <slash>
  728. to delimit the strings. If the delimiter is not
  729. .BR 'n' ,
  730. within
  731. .IR string1
  732. and
  733. .IR string2 ,
  734. the delimiter itself can be used as a literal character if it is
  735. preceded by a
  736. <backslash>.
  737. If a
  738. <backslash>
  739. character is immediately followed by a
  740. <backslash>
  741. character in
  742. .IR string1
  743. or
  744. .IR string2 ,
  745. the two
  746. <backslash>
  747. characters shall be counted as a single literal
  748. <backslash>
  749. character. The meaning of a
  750. <backslash>
  751. followed by any character that is not
  752. .BR 'n' ,
  753. a
  754. <backslash>,
  755. or the delimiter character is undefined.
  756. .IP "\fB[\fI0addr\fB]:\fIlabel\fR" 10
  757. Do nothing. This command bears a
  758. .IR label
  759. to which the
  760. .BR b
  761. and
  762. .BR t
  763. commands branch.
  764. .IP "\fB[\fI1addr\fB]=\fR" 10
  765. Write the following to standard output:
  766. .RS 10
  767. .sp
  768. .RS 4
  769. .nf
  770. "%d\en", <\fIcurrent line number\fR>
  771. .fi
  772. .P
  773. .RE
  774. .RE
  775. .IP "\fB[\fI0addr\fB]\fR" 10
  776. Ignore this empty command.
  777. .IP "\fB[\fI0addr\fB]#\fR" 10
  778. Ignore the
  779. .BR '#'
  780. and the remainder of the line (treat them as a comment), with the
  781. single exception that if the first two characters in the script are
  782. .BR \(dq#n\(dq ,
  783. the default output shall be suppressed; this shall be the equivalent of
  784. specifying
  785. .BR \-n
  786. on the command line.
  787. .SH "EXIT STATUS"
  788. The following exit values shall be returned:
  789. .IP "\00" 6
  790. Successful completion.
  791. .IP >0 6
  792. An error occurred.
  793. .SH "CONSEQUENCES OF ERRORS"
  794. Default.
  795. .LP
  796. .IR "The following sections are informative."
  797. .SH "APPLICATION USAGE"
  798. Regular expressions match entire strings, not just individual lines,
  799. but a
  800. <newline>
  801. is matched by
  802. .BR '\en'
  803. in a
  804. .IR sed
  805. RE; a
  806. <newline>
  807. is not allowed by the general definition of regular expression in
  808. POSIX.1\(hy2008. Also note that
  809. .BR '\en'
  810. cannot be used to match a
  811. <newline>
  812. at the end of an arbitrary input line;
  813. <newline>
  814. characters appear in the pattern space as a result of the
  815. .BR N
  816. editing command.
  817. .P
  818. When using
  819. .IR sed
  820. to process pathnames, it is recommended that LC_ALL, or at least
  821. LC_CTYPE and LC_COLLATE, are set to POSIX or C in the environment,
  822. since pathnames can contain byte sequences that do not form valid
  823. characters in some locales, in which case the utility's behavior would
  824. be undefined. In the POSIX locale each byte is a valid single-byte
  825. character, and therefore this problem is avoided.
  826. .SH EXAMPLES
  827. This
  828. .IR sed
  829. script simulates the BSD
  830. .IR cat
  831. .BR \-s
  832. command, squeezing excess empty lines from standard input.
  833. .sp
  834. .RS 4
  835. .nf
  836. sed -n \(aq
  837. # Write non-empty lines.
  838. /./ {
  839. p
  840. d
  841. }
  842. # Write a single empty line, then look for more empty lines.
  843. /\(ha$/ p
  844. # Get next line, discard the held <newline> (empty line),
  845. # and look for more empty lines.
  846. :Empty
  847. /\(ha$/ {
  848. N
  849. s/.//
  850. b Empty
  851. }
  852. # Write the non-empty line before going back to search
  853. # for the first in a set of empty lines.
  854. p
  855. \&\(aq
  856. .fi
  857. .P
  858. .RE
  859. .P
  860. The following
  861. .IR sed
  862. command is a much simpler method of squeezing empty lines, although
  863. it is not quite the same as
  864. .IR cat
  865. .BR \-s
  866. since it removes any initial empty lines:
  867. .sp
  868. .RS 4
  869. .nf
  870. sed -n \(aq/./,/\(ha$/p\(aq
  871. .fi
  872. .P
  873. .RE
  874. .SH RATIONALE
  875. This volume of POSIX.1\(hy2017 requires implementations to support at least ten distinct
  876. .IR wfile s,
  877. matching historical practice on many implementations. Implementations
  878. are encouraged to support more, but conforming applications should not
  879. exceed this limit.
  880. .P
  881. The exit status codes specified here are different from those in System
  882. V. System V returns 2 for garbled
  883. .IR sed
  884. commands, but returns zero with its usage message or if the input file
  885. could not be opened. The standard developers considered this to be a
  886. bug.
  887. .P
  888. The manner in which the
  889. .BR l
  890. command writes non-printable characters was changed to avoid
  891. the historical backspace-overstrike method, and other requirements to
  892. achieve unambiguous output were added. See the RATIONALE for
  893. .IR "\fIed\fR\^"
  894. for details of the format chosen, which is the same as that chosen for
  895. .IR sed .
  896. .P
  897. This volume of POSIX.1\(hy2017 requires implementations to provide pattern and hold spaces of at
  898. least 8\|192 bytes, larger than the 4\|000 bytes spaces used by some
  899. historical implementations, but less than the 20\|480 bytes limit used
  900. in an early proposal. Implementations are encouraged to allocate
  901. dynamically larger pattern and hold spaces as needed.
  902. .P
  903. The requirements for acceptance of
  904. <blank>
  905. and
  906. <space>
  907. characters in command lines has been made more explicit than in early
  908. proposals to describe clearly the historical practice and to remove
  909. confusion about the phrase ``protect initial blanks [\fIsic\fP] and tabs
  910. from the stripping that is done on every script line'' that appears in
  911. much of the historical documentation of the
  912. .IR sed
  913. utility description of text. (Not all implementations are known to have
  914. stripped
  915. <blank>
  916. characters from text lines, although they all have allowed leading
  917. <blank>
  918. characters preceding the address on a command line.)
  919. .P
  920. The treatment of
  921. .BR '#'
  922. comments differs from the SVID which only allows a comment as the first
  923. line of the script, but matches BSD-derived implementations. The
  924. comment character is treated as a command, and it has the same
  925. properties in terms of being accepted with leading
  926. <blank>
  927. characters; the BSD implementation has historically supported this.
  928. .P
  929. Early proposals required that a
  930. .IR script_file
  931. have at least one non-comment line. Some historical implementations
  932. have behaved in unexpected ways if this were not the case. The standard
  933. developers considered that this was incorrect behavior and that
  934. application developers should not have to avoid this feature. A correct
  935. implementation of this volume of POSIX.1\(hy2017 shall permit
  936. .IR script_file s
  937. that consist only of comment lines.
  938. .P
  939. Early proposals indicated that if
  940. .BR \-e
  941. and
  942. .BR \-f
  943. options were intermixed, all
  944. .BR \-e
  945. options were processed before any
  946. .BR \-f
  947. options. This has been changed to process them in the order presented
  948. because it matches historical practice and is more intuitive.
  949. .P
  950. The treatment of the
  951. .BR p
  952. flag to the
  953. .BR s
  954. command differs between System V and BSD-based systems when the default
  955. output is suppressed. In the two examples:
  956. .sp
  957. .RS 4
  958. .nf
  959. echo a | sed \(aqs/a/A/p\(aq
  960. echo a | sed -n \(aqs/a/A/p\(aq
  961. .fi
  962. .P
  963. .RE
  964. .P
  965. this volume of POSIX.1\(hy2017, BSD, System V documentation, and the SVID indicate that the
  966. first example should write two lines with
  967. .BR A ,
  968. whereas the second should write one. Some System V systems write the
  969. .BR A
  970. only once in both examples because the
  971. .BR p
  972. flag is ignored if the
  973. .BR \-n
  974. option is not specified.
  975. .P
  976. This is a case of a diametrical difference between systems that could
  977. not be reconciled through the compromise of declaring the behavior to
  978. be unspecified. The SVID/BSD/System V documentation behavior was
  979. adopted for this volume of POSIX.1\(hy2017 because:
  980. .IP " *" 4
  981. No known documentation for any historic system describes the
  982. interaction between the
  983. .BR p
  984. flag and the
  985. .BR \-n
  986. option.
  987. .IP " *" 4
  988. The selected behavior is more correct as there is no technical
  989. justification for any interaction between the
  990. .BR p
  991. flag and the
  992. .BR \-n
  993. option. A relationship between
  994. .BR \-n
  995. and the
  996. .BR p
  997. flag might imply that they are only used together, but this ignores
  998. valid scripts that interrupt the cyclical nature of the processing
  999. through the use of the
  1000. .BR D ,
  1001. .BR d ,
  1002. .BR q ,
  1003. or branching commands. Such scripts rely on the
  1004. .BR p
  1005. suffix to write the pattern space because they do not make use of the
  1006. default output at the ``bottom'' of the script.
  1007. .IP " *" 4
  1008. Because the
  1009. .BR \-n
  1010. option makes the
  1011. .BR p
  1012. flag unnecessary, any interaction would only be useful if
  1013. .IR sed
  1014. scripts were written to run both with and without the
  1015. .BR \-n
  1016. option. This is believed to be unlikely. It is even more unlikely that
  1017. programmers have coded the
  1018. .BR p
  1019. flag expecting it to be unnecessary. Because the interaction was not
  1020. documented, the likelihood of a programmer discovering the interaction
  1021. and depending on it is further decreased.
  1022. .IP " *" 4
  1023. Finally, scripts that break under the specified behavior produce too
  1024. much output instead of too little, which is easier to diagnose and
  1025. correct.
  1026. .P
  1027. The form of the substitute command that uses the
  1028. .BR n
  1029. suffix was limited to the first 512 matches in an early proposal. This
  1030. limit has been removed because there is no reason an editor processing
  1031. lines of
  1032. {LINE_MAX}
  1033. length should have this restriction. The command
  1034. .BR "s/a/A/2047"
  1035. should be able to substitute the 2\|047th occurrence of
  1036. .BR a
  1037. on a line.
  1038. .P
  1039. The
  1040. .BR b ,
  1041. .BR t ,
  1042. and
  1043. .BR :
  1044. commands are documented to ignore leading white space, but no mention
  1045. is made of trailing white space. Historical implementations of
  1046. .IR sed
  1047. assigned different locations to the labels
  1048. .BR 'x'
  1049. and
  1050. .BR \(dqx\ \(dq .
  1051. This is not useful, and leads to subtle programming errors, but it is
  1052. historical practice, and changing it could theoretically break working
  1053. scripts. Implementors are encouraged to provide warning messages about
  1054. labels that are never referenced by a
  1055. .BR b
  1056. or
  1057. .BR t
  1058. command, jumps to labels that do not exist, and label arguments that
  1059. are subject to truncation.
  1060. .P
  1061. Earlier versions of this standard allowed for implementations with
  1062. bytes other than eight bits, but this has been modified in this
  1063. version.
  1064. .SH "FUTURE DIRECTIONS"
  1065. None.
  1066. .SH "SEE ALSO"
  1067. .IR "\fIawk\fR\^",
  1068. .IR "\fIed\fR\^",
  1069. .IR "\fIgrep\fR\^"
  1070. .P
  1071. The Base Definitions volume of POSIX.1\(hy2017,
  1072. .IR "Table 5-1" ", " "Escape Sequences and Associated Actions",
  1073. .IR "Chapter 8" ", " "Environment Variables",
  1074. .IR "Section 9.3" ", " "Basic Regular Expressions",
  1075. .IR "Section 12.2" ", " "Utility Syntax Guidelines"
  1076. .\"
  1077. .SH COPYRIGHT
  1078. Portions of this text are reprinted and reproduced in electronic form
  1079. from IEEE Std 1003.1-2017, Standard for Information Technology
  1080. -- Portable Operating System Interface (POSIX), The Open Group Base
  1081. Specifications Issue 7, 2018 Edition,
  1082. Copyright (C) 2018 by the Institute of
  1083. Electrical and Electronics Engineers, Inc and The Open Group.
  1084. In the event of any discrepancy between this version and the original IEEE and
  1085. The Open Group Standard, the original IEEE and The Open Group Standard
  1086. is the referee document. The original Standard can be obtained online at
  1087. http://www.opengroup.org/unix/online.html .
  1088. .PP
  1089. Any typographical or formatting errors that appear
  1090. in this page are most likely
  1091. to have been introduced during the conversion of the source files to
  1092. man page format. To report such errors, see
  1093. https://www.kernel.org/doc/man-pages/reporting_bugs.html .