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