are used to perform one or more editing commands on all lines that either
As the simplest example, the command
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,
prints all the formatting commands in a file (lines that begin with `\*.').
except that it operates on those line that do
contain an occurrence of the pattern.
(Don't look too hard for mnemonic significance to
prints all the lines that don't begin with `\*.' _
deletes all lines that begin with `\*.',
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
Notice that we used `//' in the substitute command to mean
`the previous pattern', in this case, `Unix'.
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
to use addresses, set dot, and so on, quite freely.
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'.
searches for each line that contains `topic', scans backwards until it finds
a line that begins `.SH' (a section heading) and prints the line
thus showing the section headings under which `topic' is mentioned.
g/^\*e\*.EQ/+,/^\*e\*.EN/-p
prints all the lines that lie between
lines beginning with `.EQ' and `.EN' formatting commands.
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.
suppose the task is to change `x' to `y' and `a' to `b' on all lines
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
You should watch out for this problem:
The remembered pattern is the last pattern that was actually
`x' (as expected), and sometimes it will be `a'
You must spell it out, like this:
It is also possible to execute
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
There is no need for a final line containing a
unless there are further commands
being done under the global.
On the other hand, it does no harm to put it in either.