1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
.\" $OpeBSD$
.\"
.\" Copyright (C) Caldera International Inc. 2001-2002.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code and documentation must retain the above
.\" copyright notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed or owned by Caldera
.\" International, Inc.
.\" 4. Neither the name of Caldera International, Inc. nor the names of other
.\" contributors may be used to endorse or promote products derived from
.\" this software without specific prior written permission.
.\"
.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\" @(#)ae4 8.1 (Berkeley) 6/8/93
.\"
.NH
GLOBAL COMMANDS
.PP
The global commands
.UL g
and
.UL v
are used to perform one or more editing commands on all lines that either
contain
.UL g ) (
or don't contain
.UL v ) (
a specified pattern.
.PP
As the simplest example, the command
.P1
g/UNIX/p
.P2
prints all lines that contain the word `UNIX'.
The pattern that goes between the slashes can be anything
that could be used in a line search or in a substitute command;
exactly the same rules and limitations apply.
.PP
As another example, then,
.P1
g/^\*e\*./p
.P2
prints all the formatting commands in a file (lines that begin with `\*.').
.PP
The
.UL v
command is identical to
.UL g ,
except that it operates on those line that do
.ul
not
contain an occurrence of the pattern.
(Don't look too hard for mnemonic significance to
the letter `v'.)
So
.P1
v/^\*e\*./p
.P2
prints all the lines that don't begin with `\*.' _
the actual text lines.
.PP
The command that follows
.UL g
or
.UL v
can be anything:
.P1
g/^\*e\*./d
.P2
deletes all lines that begin with `\*.',
and
.P1
g/^$/d
.P2
deletes all empty lines.
.PP
Probably the most useful command that can follow a global is the
substitute command, for this can be used to make a change
and print each affected line for verification.
For example, we could change the word `Unix' to `UNIX'
everywhere, and verify that
it really worked,
with
.P1
g/Unix/s//UNIX/gp
.P2
Notice that we used `//' in the substitute command to mean
`the previous pattern', in this case, `Unix'.
The
.UL p
command is done on every line
that matches the pattern,
not just those on which a substitution took place.
.PP
The global command operates by making
two passes over the file.
On the first pass, all lines that match the pattern are marked.
On the second pass, each marked line in turn is examined,
dot is set to that line, and the command executed.
This means that it is possible for the command that follows a
.UL g
or
.UL v
to use addresses, set dot, and so on, quite freely.
.P1
g/^\*e\*.PP/+
.P2
prints the line that follows each `.PP' command (the signal for
a new paragraph in some formatting packages).
Remember that `+' means `one line past dot'.
And
.P1
g/topic/?^\*e\*.SH?1
.P2
searches for each line that contains `topic', scans backwards until it finds
a line that begins `.SH' (a section heading) and prints the line
that follows that,
thus showing the section headings under which `topic' is mentioned.
Finally,
.P1
g/^\*e\*.EQ/+,/^\*e\*.EN/-p
.P2
prints all the lines that lie between
lines beginning with `.EQ' and `.EN' formatting commands.
.PP
The
.UL g
and
.UL v
commands can also be
preceded by line numbers, in which case the lines searched
are only those in the range specified.
.SH
Multi-line Global Commands
.PP
It is possible to do more than one command under the control of a
global command, although the syntax for expressing the operation
is not especially natural or pleasant.
As an example,
suppose the task is to change `x' to `y' and `a' to `b' on all lines
that contain `thing'.
Then
.P1
g/thing/s/x/y/\*e
s/a/b/
.P2
is sufficient.
The `\*e' signals the
.UL g
command that the set of commands continues on the next line;
it terminates on the first line that does not end with `\*e'.
(As a minor blemish, you can't use a substitute command
to insert a newline within a
.UL g
command.)
.PP
You should watch out for this problem:
the command
.P1
g/x/s//y/\*e
s/a/b/
.P2
does
.ul
not
work as you expect.
The remembered pattern is the last pattern that was actually
executed,
so sometimes it will be
`x' (as expected), and sometimes it will be `a'
(not expected).
You must spell it out, like this:
.P1
g/x/s/x/y/\*e
s/a/b/
.P2
.PP
It is also possible to execute
.UL a ,
.UL c
and
.UL i
commands under a global command; as with other multi-line constructions,
all that is needed is to add a `\*e' at the end of each line except the last.
Thus to add a `.nf' and `.sp' command before each `.EQ' line, type
.P1
g/^\*e\*.EQ/i\*e
\&\*.nf\*e
\&\*.sp
.P2
There is no need for a final line containing a
`\*.' to terminate the
.UL i
command,
unless there are further commands
being done under the global.
On the other hand, it does no harm to put it in either.
|