home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / edtut / e4 < prev    next >
Encoding:
Text File  |  1979-01-10  |  5.5 KB  |  268 lines

  1. .SH
  2. Exercise 5:
  3. .PP
  4. Experiment with the substitute command.
  5. See what happens if you
  6. substitute for some word on a line with several occurrences of that word.
  7. For example, do this:
  8. .P1
  9. a
  10. the other side of the coin
  11. \*.
  12. s/the/on the/p
  13. .P2
  14. You will get
  15. .P1
  16. on the other side of the coin
  17. .P2
  18. A substitute command changes only the first occurrence of the first string.
  19. You can change all occurrences by adding a
  20. .UL g
  21. (for ``global'')
  22. to the
  23. .UL s
  24. command, like this:
  25. .P1
  26. s/ . . . / . . . /gp
  27. .P2
  28. Try other characters instead of slashes to delimit the two sets
  29. of characters in the
  30. .UL s
  31. command \- anything should work
  32. except blanks or tabs.
  33. .PP
  34. (If you get funny results using any of the characters
  35. .P1
  36. ^    \*.    $    [    *    \e    &
  37. .P2
  38. read the section on ``Special Characters''.)
  39. .SH
  40. Context searching \- ``/ . . . /''
  41. .PP
  42. With the substitute command mastered, you can move on to
  43. another highly important idea of
  44. .ul
  45. ed
  46. \- context searching.
  47. .PP
  48. Suppose you have the original three line text in the buffer:
  49. .P1
  50. Now is the time
  51. for all good men
  52. to come to the aid of their party.
  53. .P2
  54. Suppose you want to find the line that contains
  55. .IT their
  56. so
  57. you can change it to
  58. .IT the .
  59. Now with only three lines in the buffer, it's pretty easy
  60. to keep track of what line the word
  61. .IT their
  62. is on.
  63. But if the buffer contained several hundred lines,
  64. and you'd been making changes, deleting and rearranging lines,
  65. and so on, you would no longer really know what this line
  66. number would be.
  67. Context searching is simply a method of specifying the desired line,
  68. regardless of what its number is,
  69. by specifying some context on it.
  70. .PP
  71. The way to say ``search for a line
  72. that contains this particular string of characters''
  73. is to type
  74. .P1
  75. /\fIstring of characters we want to find\fP/
  76. .P2
  77. For example,
  78. the
  79. .ul
  80. ed
  81. command
  82. .P1
  83. /their/
  84. .P2
  85. is a context search which
  86. is sufficient to find the desired line \-
  87. it will locate the next occurrence of
  88. the characters between slashes (``their'').
  89. It also sets dot to that line
  90. and prints the line for verification:
  91. .P1
  92. to come to the aid of their party.
  93. .P2
  94. ``Next occurrence'' means that
  95. .ul
  96. ed
  97. starts looking for the string at line
  98. .UL .+1 ,
  99. searches to the end of the buffer,
  100. then continues at line 1 and searches to line dot.
  101. (That is, the search ``wraps around'' from
  102. .UL $
  103. to
  104. 1.)
  105. It scans all the lines in the buffer until it either finds the desired line
  106. or gets back to dot again.
  107. If the given string of characters can't be found in any line,
  108. .ul
  109. ed
  110. types the error message
  111. .P1
  112. ?
  113. .P2
  114. Otherwise it prints the line it found.
  115. .PP
  116. You can do both the search for the desired line
  117. .ul
  118. and
  119. a
  120. substitution all at once, like this:
  121. .P1
  122. /their/s/their/the/p
  123. .P2
  124. which will yield
  125. .P1
  126. to come to the aid of the party.
  127. .P2
  128. There were three parts to that last command:
  129. context search for the desired line, make the substitution, print the line.
  130. .PP
  131. The expression
  132. .UL /their/
  133. is a context search expression.
  134. In their simplest form,
  135. all context search expressions are like this \-
  136. a string of characters surrounded by slashes.
  137. Context searches are interchangeable with line numbers,
  138. so they can be used by themselves to find and print a desired line,
  139. or as line numbers for some other command, like
  140. .UL s .
  141. They were used both ways in the examples above.
  142. .PP
  143. Suppose the buffer contains the three familiar lines
  144. .P1
  145. Now is the time
  146. for all good men
  147. to come to the aid of their party.
  148. .P2
  149. Then the
  150. .ul
  151. ed
  152. line numbers
  153. .P1
  154. /Now/+1
  155. /good/
  156. /party/\-1
  157. .P2
  158. are all context search expressions, and they all refer
  159. to the same line (line 2).
  160. To make a change in line 2,
  161. you could say
  162. .P1
  163. /Now/+1s/good/bad/
  164. .P2
  165. or
  166. .P1
  167. /good/s/good/bad/
  168. .P2
  169. or
  170. .P1
  171. /party/\-1s/good/bad/
  172. .P2
  173. The choice is dictated only by convenience.
  174. You could print all three lines by, for instance
  175. .P1
  176. /Now/,/party/p
  177. .P2
  178. or
  179. .P1
  180. /Now/,/Now/+2p
  181. .P2
  182. or by any number of similar combinations.
  183. The first one of these might be better if you don't
  184. know how many lines are involved.
  185. (Of course, if there were only three lines in the buffer,
  186. you'd use
  187. .P1
  188. 1,$p
  189. .P2
  190. but not if there were several hundred.)
  191. .PP
  192. The basic rule is: a context search expression is
  193. .ul
  194. the same as
  195. a line number, so it can be used wherever a line number is needed.
  196. .SH
  197. Exercise 6:
  198. .PP
  199. Experiment with context searching.
  200. Try a body of text with
  201. several occurrences
  202. of the same string of characters, and scan through it using
  203. the same context search.
  204. .PP
  205. Try using context searches as line numbers for the
  206. substitute, print and delete commands.
  207. (They can also be used
  208. with
  209. .UL r ,
  210. .UL w ,
  211. and
  212. .UL a .)
  213. .PP
  214. Try context searching using
  215. .UL ?text?
  216. instead of
  217. .UL /text/ .
  218. This scans lines in the buffer in reverse order
  219. rather than normal.
  220. This is
  221. sometimes useful if you go too far while looking for some
  222. string of characters \- it's an easy way to back up.
  223. .PP
  224. (If you get funny results with any of the characters
  225. .P1
  226. ^    \*.    $    [    *    \e    &
  227. .P2
  228. read the section on ``Special Characters''.)
  229. .PP
  230. .ul
  231. Ed
  232. provides a shorthand for repeating a context search
  233. for the same string.
  234. For example,
  235. the
  236. .ul
  237. ed
  238. line number
  239. .P1
  240. /string/
  241. .P2
  242. will find the next occurrence of
  243. .UL string .
  244. It often happens that this is not the desired line,
  245. so the search must be repeated.
  246. This can be done by typing merely
  247. .P1
  248. //
  249. .P2
  250. This shorthand stands for ``the most recently used
  251. context search expression.''
  252. It can
  253. also be used as the first string of the substitute
  254. command, as in
  255. .P1
  256. /string1/s//string2/
  257. .P2
  258. which will find the next occurrence of
  259. .UL string1
  260. and replace it by
  261. .UL string2 .
  262. This can save a lot of typing.
  263. Similarly
  264. .P1
  265. ??
  266. .P2
  267. means ``scan backwards for the same expression.''
  268.