include problem
[unix-history] / .ref-BSD-3 / usr / doc / adv.ed / ae4
CommitLineData
8340f87c
BJ
1.NH
2GLOBAL COMMANDS
3.PP
4The global commands
5.UL g
6and
7.UL v
8are used to perform one or more editing commands on all lines that either
9contain
10.UL g ) (
11or don't contain
12.UL v ) (
13a specified pattern.
14.PP
15As the simplest example, the command
16.P1
17g/UNIX/p
18.P2
19prints all lines that contain the word `UNIX'.
20The pattern that goes between the slashes can be anything
21that could be used in a line search or in a substitute command;
22exactly the same rules and limitations apply.
23.PP
24As another example, then,
25.P1
26g/^\*e\*./p
27.P2
28prints all the formatting commands in a file (lines that begin with `\*.').
29.PP
30The
31.UL v
32command is identical to
33.UL g ,
34except that it operates on those line that do
35.ul
36not
37contain an occurrence of the pattern.
38(Don't look too hard for mnemonic significance to
39the letter `v'.)
40So
41.P1
42v/^\*e\*./p
43.P2
44prints all the lines that don't begin with `\*.' _
45the actual text lines.
46.PP
47The command that follows
48.UL g
49or
50.UL v
51can be anything:
52.P1
53g/^\*e\*./d
54.P2
55deletes all lines that begin with `\*.',
56and
57.P1
58g/^$/d
59.P2
60deletes all empty lines.
61.PP
62Probably the most useful command that can follow a global is the
63substitute command, for this can be used to make a change
64and print each affected line for verification.
65For example, we could change the word `Unix' to `UNIX'
66everywhere, and verify that
67it really worked,
68with
69.P1
70g/Unix/s//UNIX/gp
71.P2
72Notice that we used `//' in the substitute command to mean
73`the previous pattern', in this case, `Unix'.
74The
75.UL p
76command is done on every line
77that matches the pattern,
78not just those on which a substitution took place.
79.PP
80The global command operates by making
81two passes over the file.
82On the first pass, all lines that match the pattern are marked.
83On the second pass, each marked line in turn is examined,
84dot is set to that line, and the command executed.
85This means that it is possible for the command that follows a
86.UL g
87or
88.UL v
89to use addresses, set dot, and so on, quite freely.
90.P1
91g/^\*e\*.PP/+
92.P2
93prints the line that follows each `.PP' command (the signal for
94a new paragraph in some formatting packages).
95Remember that `+' means `one line past dot'.
96And
97.P1
98g/topic/?^\*e\*.SH?1
99.P2
100searches for each line that contains `topic', scans backwards until it finds
101a line that begins `.SH' (a section heading) and prints the line
102that follows that,
103thus showing the section headings under which `topic' is mentioned.
104Finally,
105.P1
106g/^\*e\*.EQ/+,/^\*e\*.EN/-p
107.P2
108prints all the lines that lie between
109lines beginning with `.EQ' and `.EN' formatting commands.
110.PP
111The
112.UL g
113and
114.UL v
115commands can also be
116preceded by line numbers, in which case the lines searched
117are only those in the range specified.
118.SH
119Multi-line Global Commands
120.PP
121It is possible to do more than one command under the control of a
122global command, although the syntax for expressing the operation
123is not especially natural or pleasant.
124As an example,
125suppose the task is to change `x' to `y' and `a' to `b' on all lines
126that contain `thing'.
127Then
128.P1
129g/thing/s/x/y/\*e
130s/a/b/
131.P2
132is sufficient.
133The `\*e' signals the
134.UL g
135command that the set of commands continues on the next line;
136it terminates on the first line that does not end with `\*e'.
137(As a minor blemish, you can't use a substitute command
138to insert a newline within a
139.UL g
140command.)
141.PP
142You should watch out for this problem:
143the command
144.P1
145g/x/s//y/\*e
146s/a/b/
147.P2
148does
149.ul
150not
151work as you expect.
152The remembered pattern is the last pattern that was actually
153executed,
154so sometimes it will be
155`x' (as expected), and sometimes it will be `a'
156(not expected).
157You must spell it out, like this:
158.P1
159g/x/s/x/y/\*e
160s/a/b/
161.P2
162.PP
163It is also possible to execute
164.UL a ,
165.UL c
166and
167.UL i
168commands under a global command; as with other multi-line constructions,
169all that is needed is to add a `\*e' at the end of each line except the last.
170Thus to add a `.nf' and `.sp' command before each `.EQ' line, type
171.P1
172g/^\*e\*.EQ/i\*e
173\&\*.nf\*e
174\&\*.sp
175.P2
176There is no need for a final line containing a
177`\*.' to terminate the
178.UL i
179command,
180unless there are further commands
181being done under the global.
182On the other hand, it does no harm to put it in either.