sed 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. .TH SED 1
  2. .SH NAME
  3. sed \- stream editor
  4. .SH SYNOPSIS
  5. .B sed
  6. [
  7. .B -n
  8. ]
  9. [
  10. .B -g
  11. ]
  12. [
  13. .B -e
  14. .I script
  15. ]
  16. [
  17. .B -f
  18. .I sfile
  19. ]
  20. [
  21. .I file ...
  22. ]
  23. .SH DESCRIPTION
  24. .I Sed
  25. copies the named
  26. .I files
  27. (standard input default) to the standard output,
  28. edited according to a script of commands.
  29. The
  30. .B -f
  31. option causes the script to be taken from file
  32. .IR sfile ;
  33. these options accumulate.
  34. If there is just one
  35. .B -e
  36. option and no
  37. .BR -f 's,
  38. the flag
  39. .B -e
  40. may be omitted.
  41. The
  42. .B -n
  43. option suppresses the default output;
  44. .B -g
  45. causes all substitutions to be global, as if suffixed
  46. .BR g .
  47. .PP
  48. A script consists of editing commands, one per line,
  49. of the following form:
  50. .IP
  51. [\fIaddress\fR [\fL,\fI address\fR] ] \fIfunction\fR [\fIargument\fR ...]
  52. .PP
  53. In normal operation
  54. .I sed
  55. cyclically copies a line of input into a
  56. .I pattern space
  57. (unless there is something left after
  58. a
  59. .L D
  60. command),
  61. applies in sequence
  62. all commands whose
  63. .I addresses
  64. select that pattern space,
  65. and at the end of the script copies the pattern space
  66. to the standard output (except under
  67. .BR -n )
  68. and deletes the pattern space.
  69. .PP
  70. An
  71. .I address
  72. is either a decimal number that counts
  73. input lines cumulatively across files, a
  74. .L $
  75. that
  76. addresses the last line of input, or a context address,
  77. .BI / regular-expression / \f1,
  78. in the style of
  79. .IR regexp (6),
  80. with the added convention that
  81. .L \en
  82. matches a
  83. newline embedded in the pattern space.
  84. .PP
  85. A command line with no addresses selects every pattern space.
  86. .PP
  87. A command line with
  88. one address selects each pattern space that matches the address.
  89. .PP
  90. A command line with
  91. two addresses selects the inclusive range from the first
  92. pattern space that matches the first address through
  93. the next pattern space that matches
  94. the second.
  95. (If the second address is a number less than or equal
  96. to the line number first selected, only one
  97. line is selected.)
  98. Thereafter the process is repeated, looking again for the
  99. first address.
  100. .PP
  101. Editing commands can be applied to non-selected pattern
  102. spaces by use of the negation function
  103. .L !
  104. (below).
  105. .PP
  106. An argument denoted
  107. .I text
  108. consists of one or more lines,
  109. all but the last of which end with
  110. .L \e
  111. to hide the
  112. newline.
  113. Backslashes in text are treated like backslashes
  114. in the replacement string of an
  115. .L s
  116. command,
  117. and may be used to protect initial blanks and tabs
  118. against the stripping that is done on
  119. every script line.
  120. .PP
  121. An argument denoted
  122. .I rfile
  123. or
  124. .I wfile
  125. must terminate the command
  126. line and must be preceded by exactly one blank.
  127. Each
  128. .I wfile
  129. is created before processing begins.
  130. There can be at most 120 distinct
  131. .I wfile
  132. arguments.
  133. .TP \w'\fL!\ \fIfunction\fLXXX'u
  134. .B a\e
  135. .br
  136. .ns
  137. .TP
  138. .I text
  139. Append.
  140. Place
  141. .I text
  142. on the output before
  143. reading the next input line.
  144. .TP
  145. .BI b " label"
  146. Branch to the
  147. .B :
  148. command bearing the
  149. .IR label .
  150. If
  151. .I label
  152. is empty, branch to the end of the script.
  153. .TP
  154. .B c\e
  155. .br
  156. .ns
  157. .TP
  158. .I text
  159. Change.
  160. Delete the pattern space.
  161. With 0 or 1 address or at the end of a 2-address range, place
  162. .I text
  163. on the output.
  164. Start the next cycle.
  165. .TP
  166. .B d
  167. Delete the pattern space.
  168. Start the next cycle.
  169. .TP
  170. .B D
  171. Delete the initial segment of the
  172. pattern space through the first newline.
  173. Start the next cycle.
  174. .TP
  175. .B g
  176. Replace the contents of the pattern space
  177. by the contents of the hold space.
  178. .TP
  179. .B G
  180. Append the contents of the hold space to the pattern space.
  181. .TP
  182. .B h
  183. Replace the contents of the hold space by the contents of the pattern space.
  184. .TP
  185. .B H
  186. Append the contents of the pattern space to the hold space.
  187. .ne 3
  188. .TP
  189. .B i\e
  190. .br
  191. .ns
  192. .TP
  193. .I text
  194. Insert.
  195. Place
  196. .I text
  197. on the standard output.
  198. .TP
  199. .B n
  200. Copy the pattern space to the standard output.
  201. Replace the pattern space with the next line of input.
  202. .TP
  203. .B N
  204. Append the next line of input to the pattern space
  205. with an embedded newline.
  206. (The current line number changes.)
  207. .TP
  208. .B p
  209. Print.
  210. Copy the pattern space to the standard output.
  211. .TP
  212. .B P
  213. Copy the initial segment of the pattern space through
  214. the first newline to the standard output.
  215. .TP
  216. .B q
  217. Quit.
  218. Branch to the end of the script.
  219. Do not start a new cycle.
  220. .TP
  221. .BI r " rfile"
  222. Read the contents of
  223. .IR rfile .
  224. Place them on the output before reading
  225. the next input line.
  226. .TP
  227. .B s/\fIregular-expression\fP/\fIreplacement\fP/\fIflags
  228. Substitute the
  229. .I replacement
  230. string for instances of the
  231. .I regular-expression
  232. in the pattern space.
  233. Any character may be used instead of
  234. .LR / .
  235. For a fuller description see
  236. .IR regexp (6).
  237. .I Flags
  238. is zero or more of
  239. .RS
  240. .TP
  241. .B g
  242. Global.
  243. Substitute for all non-overlapping instances of the
  244. .I regular expression
  245. rather than just the
  246. first one.
  247. .TP
  248. .B p
  249. Print the pattern space if a replacement was made.
  250. .TP
  251. .BI w " wfile"
  252. Write.
  253. Append the pattern space to
  254. .I wfile
  255. if a replacement
  256. was made.
  257. .RE
  258. .TP
  259. .BI t " label"
  260. Test.
  261. Branch to the
  262. .L :
  263. command bearing the
  264. .I label
  265. if any
  266. substitutions have been made since the most recent
  267. reading of an input line or execution of a
  268. .LR t .
  269. If
  270. .I label
  271. is empty, branch to the end of the script.
  272. .TP
  273. .B w
  274. .I wfile
  275. .br
  276. Write.
  277. Append the pattern space to
  278. .IR wfile .
  279. .TP
  280. .B x
  281. Exchange the contents of the pattern and hold spaces.
  282. .TP
  283. .B y/\fIstring1\fP/\fIstring2\fP/
  284. Transform.
  285. Replace all occurrences of characters in
  286. .I string1
  287. with the corresponding character in
  288. .IR string2 .
  289. The lengths of
  290. .I
  291. string1
  292. and
  293. .I string2
  294. must be equal.
  295. .TP
  296. .BI ! "function"
  297. Don't.
  298. Apply the
  299. .I function
  300. (or group, if
  301. .I function
  302. is
  303. .LR { )
  304. only to lines
  305. .I not
  306. selected by the address(es).
  307. .TP
  308. .BI : " label"
  309. This command does nothing; it bears a
  310. .I label
  311. for
  312. .B b
  313. and
  314. .B t
  315. commands to branch to.
  316. .TP
  317. .B =
  318. Place the current line number on the standard output as a line.
  319. .TP
  320. .B {
  321. Execute the following commands through a matching
  322. .L }
  323. only when the pattern space is selected.
  324. .TP
  325. .B " "
  326. An empty command is ignored.
  327. .ne 4
  328. .SH EXAMPLES
  329. .TP
  330. .B sed 10q file
  331. Print the first 10 lines of the file.
  332. .TP
  333. .B sed '/^$/d'
  334. Delete empty lines from standard input.
  335. .TP
  336. .B sed 's/UNIX/& system/g'
  337. Replace every instance of
  338. .L UNIX
  339. by
  340. .LR "UNIX system" .
  341. .PP
  342. .EX
  343. sed 's/ *$// \fRdrop trailing blanks\fP
  344. /^$/d \fRdrop empty lines\fP
  345. s/ */\e \fRreplace blanks by newlines\fP
  346. /g
  347. /^$/d' chapter*
  348. .EE
  349. .ns
  350. .IP
  351. Print the files
  352. .BR chapter1 ,
  353. .BR chapter2 ,
  354. etc. one word to a line.
  355. .PP
  356. .EX
  357. nroff -ms manuscript | sed '
  358. ${
  359. /^$/p \fRif last line of file is empty, print it\fP
  360. }
  361. //N \fRif current line is empty, append next line\fP
  362. /^\en$/D' \fRif two lines are empty, delete the first\fP
  363. .EE
  364. .ns
  365. .IP
  366. Delete all but one of each group of empty lines from a
  367. formatted manuscript.
  368. .SH SOURCE
  369. .B /sys/src/cmd/sed.c
  370. .SH SEE ALSO
  371. .IR ed (1),
  372. .IR grep (1),
  373. .IR awk (1),
  374. .IR lex (1),
  375. .IR sam (1),
  376. .IR regexp (6)
  377. .br
  378. L. E. McMahon,
  379. `SED \(em A Non-interactive Text Editor',
  380. Unix Research System Programmer's Manual, Volume 2.
  381. .SH BUGS
  382. If input is from a pipe, buffering may consume
  383. characters beyond a line on which a
  384. .L q
  385. command is executed.