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