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