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