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