1
0

sed 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 option
  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 ...] [\fL;\fP]
  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\fL'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. as per
  234. .I regsub
  235. in
  236. .IR regexp (2).
  237. Any character may be used instead of
  238. .LR / .
  239. For a fuller description see
  240. .IR regexp (6).
  241. .I Flags
  242. is zero or more of
  243. .RS
  244. .TP
  245. .B g
  246. Global.
  247. Substitute for all non-overlapping instances of the
  248. .I regular expression
  249. rather than just the
  250. first one.
  251. .TP
  252. .B p
  253. Print the pattern space if a replacement was made.
  254. .TF "w\fI wfile "
  255. .PD
  256. .TP
  257. .BI w " wfile"
  258. Write.
  259. Append the pattern space to
  260. .I wfile
  261. if a replacement
  262. was made.
  263. .RE
  264. .TP
  265. .BI t " label"
  266. Test.
  267. Branch to the
  268. .L :
  269. command bearing the
  270. .I label
  271. if any
  272. substitutions have been made since the most recent
  273. reading of an input line or execution of a
  274. .LR t .
  275. If
  276. .I label
  277. is empty, branch to the end of the script.
  278. .TP
  279. .BI w " wfile"
  280. Write.
  281. Append the pattern space to
  282. .IR wfile .
  283. .TP
  284. .B x
  285. Exchange the contents of the pattern and hold spaces.
  286. .TP
  287. .B y/\fIstring1\fP/\fIstring2\fP/
  288. Transform.
  289. Replace all occurrences of characters in
  290. .I string1
  291. with the corresponding character in
  292. .IR string2 .
  293. The lengths of
  294. .I
  295. string1
  296. and
  297. .I string2
  298. must be equal.
  299. .TP
  300. .BI ! "function"
  301. Don't.
  302. Apply the
  303. .I function
  304. (or group, if
  305. .I function
  306. is
  307. .LR { )
  308. only to lines
  309. .I not
  310. selected by the address(es).
  311. .TP
  312. .B #
  313. Comment.
  314. Ignore the rest of the line.
  315. .TP
  316. .BI : " label"
  317. This command does nothing; it bears a
  318. .I label
  319. for
  320. .B b
  321. and
  322. .B t
  323. commands to branch to.
  324. .TP
  325. .B =
  326. Place the current line number on the standard output as a line.
  327. .TP
  328. .B {
  329. Execute the following commands through a matching
  330. .L }
  331. only when the pattern space is selected.
  332. .TP
  333. .B " "
  334. An empty command is ignored.
  335. .ne 4
  336. .SH EXAMPLES
  337. .TP
  338. .B sed 10q file
  339. Print the first 10 lines of the file.
  340. .TP
  341. .B sed '/^$/d'
  342. Delete empty lines from standard input.
  343. .TP
  344. .B sed 's/UNIX/& system/g'
  345. Replace every instance of
  346. .L UNIX
  347. by
  348. .LR "UNIX system" .
  349. .PP
  350. .EX
  351. sed 's/ *$// \fRdrop trailing blanks\fP
  352. /^$/d \fRdrop empty lines\fP
  353. s/ */\e \fRreplace blanks by newlines\fP
  354. /g
  355. /^$/d' chapter*
  356. .EE
  357. .ns
  358. .IP
  359. Print the files
  360. .BR chapter1 ,
  361. .BR chapter2 ,
  362. etc. one word to a line.
  363. .PP
  364. .EX
  365. nroff -ms manuscript | sed '
  366. ${
  367. /^$/p \fRif last line of file is empty, print it\fP
  368. }
  369. //N \fRif current line is empty, append next line\fP
  370. /^\en$/D' \fRif two lines are empty, delete the first\fP
  371. .EE
  372. .ns
  373. .IP
  374. Delete all but one of each group of empty lines from a
  375. formatted manuscript.
  376. .SH SOURCE
  377. .B /sys/src/cmd/sed.c
  378. .SH SEE ALSO
  379. .IR ed (1),
  380. .IR grep (1),
  381. .IR awk (1),
  382. .IR lex (1),
  383. .IR sam (1),
  384. .IR regexp (6)
  385. .br
  386. L. E. McMahon,
  387. `SED \(em A Non-interactive Text Editor',
  388. Unix Research System Programmer's Manual, Volume 2.
  389. .SH BUGS
  390. If input is from a pipe, buffering may consume
  391. characters beyond a line on which a
  392. .L q
  393. command is executed.