4.  GLOBAL COMMANDS

      The global commands g and v are used to perform one or more editing commands on all lines that either contain (g) or don't contain (v) a specified pattern.

      As the simplest example, the command g/UNIX/p
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.

      As another example, then, g/^\\./p
prints all the formatting commands in a file (lines that begin with `.').

      The v command is identical to g, except that it operates on those line that do not contain an occurrence of the pattern. (Don't look too hard for mnemonic significance to the letter `v'.) So v/^\\./p
prints all the lines that don't begin with `.' _
the actual text lines.

      The command that follows g or v can be anything: g/^\\./d
deletes all lines that begin with `.',
and
g/^$/d
deletes all empty lines.

      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 g/Unix/s//UNIX/gp
Notice that we used `//' in the substitute command to mean
`the previous pattern', in this case, `Unix'.
The
p
command is done on every line
that matches the pattern,
not just those on which a substitution took place.

      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 g or v to use addresses, set dot, and so on, quite freely. g/^\\.PP/+
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
g/topic/?^\\.SH?1
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,
g/^\\.EQ/+,/^\\.EN/-p
prints all the lines that lie between
lines beginning with `.EQ' and `.EN' formatting commands.

      The g and v commands can also be preceded by line numbers, in which case the lines searched are only those in the range specified.

Multi-line Global Commands

      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 g/thing/s/x/y/\\
s/a/b/
is sufficient.
The `\\' signals the
g
command that the set of commands continues on the next line;
it terminates on the first line that does not end with `\\'.
(As a minor blemish, you can't use a substitute command
to insert a newline within a
g
command.)

      You should watch out for this problem: the command g/x/s//y/\\
s/a/b/
does
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:
g/x/s/x/y/\\
s/a/b/

      It is also possible to execute a, c and i commands under a global command; as with other multi-line constructions, all that is needed is to add a `\\' at the end of each line except the last. Thus to add a `.nf' and `.sp' command before each `.EQ' line, type g/^\\.EQ/i\\
.nf\\
.sp
There is no need for a final line containing a
`.' to terminate the
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.