home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / 1_1_contri / usd / 13_edadv / ae7 < prev    next >
Encoding:
Text File  |  1986-06-05  |  4.4 KB  |  187 lines

  1. .\"    @(#)ae7    6.1 (Berkeley) 6/5/86
  2. .\"
  3. .NH
  4. SUPPORTING TOOLS
  5. .PP
  6. There are several tools and techniques that go along with the
  7. editor, all of which are relatively easy once you
  8. know how
  9. .UL ed
  10. works,
  11. because they are all based on the editor.
  12. In this section we will give some fairly cursory examples
  13. of these tools,
  14. more to indicate their existence than to provide
  15. a complete tutorial.
  16. More information on each can be found in
  17. [3].
  18. .SH
  19. Grep
  20. .PP
  21. Sometimes you want to find all occurrences of some word or pattern in
  22. a set of files, to edit them
  23. or perhaps just to verify their presence or absence.
  24. It may be possible to edit each file separately and look
  25. for the pattern of interest, but if there are many files
  26. this can get very tedious,
  27. and if the files are really big,
  28. it may be impossible because of limits in 
  29. .UL ed .
  30. .PP
  31. The program
  32. .UL grep
  33. was invented to get around these limitations.
  34. The search patterns that we have described in the paper are often
  35. called `regular expressions', and
  36. `grep' stands for
  37. .P1
  38. g/re/p
  39. .P2
  40. That describes exactly what
  41. .UL grep
  42. does _
  43. it prints every line in a set of files that contains a
  44. particular pattern.
  45. Thus
  46. .P1
  47. grep  \(fmthing\(fm  file1  file2  file3  ...
  48. .P2
  49. finds `thing' wherever it occurs in any of the files
  50. `file1',
  51. `file2',
  52. etc.
  53. .UL grep
  54. also indicates the file in which the line was found,
  55. so you can later edit it if you like.
  56. .PP
  57. The pattern represented by `thing' can be any
  58. pattern you can use in the editor,
  59. since
  60. .UL grep
  61. and
  62. .UL ed
  63. use exactly the same mechanism for
  64. pattern searching.
  65. It is wisest always to enclose the pattern in the
  66. single quotes \(fm...\(fm if it contains any non-alphabetic
  67. characters, since many such characters also mean something
  68. special to the
  69. .UX
  70. command interpreter
  71. (the `shell').
  72. If you don't quote them, the command interpreter will
  73. try to interpret them before
  74. .UL grep
  75. gets a chance.
  76. .PP
  77. There is also a way to find lines that
  78. .ul
  79. don't 
  80. contain a pattern:
  81. .P1
  82. grep  -v  \(fmthing\(fm  file1  file2  ...
  83. .P2
  84. finds all lines that
  85. don't contains `thing'.
  86. The
  87. .UL \-v
  88. must occur in the position shown.
  89. Given
  90. .UL grep
  91. and
  92. .UL grep\ \-v ,
  93. it is possible to do things like selecting all lines that
  94. contain some combination of patterns.
  95. For example, to get all lines that contain `x' but not `y':
  96. .P1
  97. grep  x  file...  |  grep  -v  y
  98. .P2
  99. (The notation | is a `pipe',
  100. which causes the output of the first command to be used as
  101. input to the second command; see [2].)
  102. .SH
  103. Editing Scripts
  104. .PP
  105. If a fairly complicated set of editing operations 
  106. is to be done on a whole set of files,
  107. the easiest thing to do is to make up a `script',
  108. i.e., a file that contains the operations you want to perform,
  109. then apply this script to each file in turn.
  110. .PP
  111. For example, suppose you want to change every
  112. `Unix' to `UNIX' and every `Gcos' to `GCOS' in a large number of files.
  113. Then put into the file `script' the lines
  114. .P1
  115. g/Unix/s//UNIX/g
  116. g/Gcos/s//GCOS/g
  117. w
  118. q
  119. .P2
  120. Now you can say
  121. .P1
  122. ed file1 <script
  123. ed file2 <script
  124. \&...
  125. .P2
  126. This causes
  127. .UL ed
  128. to take its commands from the prepared script.
  129. Notice that the whole job has to be planned in advance.
  130. .PP
  131. And of course by using the
  132. .UX
  133. command interpreter, you can
  134. cycle through a set of files
  135. automatically, with varying degrees of ease.
  136. .SH
  137. Sed
  138. .PP
  139. .UL sed
  140. (`stream editor')
  141. is a version of the editor with restricted capabilities
  142. but which is capable of processing unlimited amounts of input.
  143. Basically
  144. .UL sed
  145. copies its input to its output, applying one or more
  146. editing commands to each line of input.
  147. .PP
  148. As an example, suppose that we want to do the `Unix' to `UNIX'
  149. part of the
  150. example given above,
  151. but without rewriting the files.
  152. Then the command
  153. .P1
  154. sed  \(fms/Unix/UNIX/g\(fm  file1  file2  ...
  155. .P2
  156. applies the command
  157. `s/Unix/UNIX/g'
  158. to all lines from `file1', `file2', etc.,
  159. and copies all lines to the output.
  160. The advantage of using
  161. .UL sed
  162. in such a case is that it can be used
  163. with input too large for
  164. .UL ed
  165. to handle.
  166. All the output can be collected in one place,
  167. either in a file or perhaps piped into another program.
  168. .PP
  169. If the editing transformation is so complicated
  170. that
  171. more than one editing command is needed,
  172. commands can be supplied from a file,
  173. or on the command line,
  174. with a slightly more complex syntax.
  175. To take commands from a file, for example,
  176. .P1
  177. sed  -f  cmdfile  input-files...
  178. .P2
  179. .PP
  180. .UL sed
  181. has further capabilities, including conditional testing
  182. and branching, which we cannot go into here, but which are
  183. described in detail in 
  184. .ul
  185. Sed \- A Non-interactive Text Editor.
  186.  
  187.