@(#)ae4 8.1 (Berkeley) 06/08/93
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
2 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,
1 g/^\*e\*./p
2 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 .ul not contain an occurrence of the pattern. (Don't look too hard for mnemonic significance to the letter `v'.) So
1 v/^\*e\*./p
2 prints all the lines that don't begin with `\*.' _ the actual text lines.
The command that follows g or v can be anything:
1 g/^\*e\*./d
2 deletes all lines that begin with `\*.', and
1 g/^$/d
2 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
2 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.
1 g/^\*e\*.PP/+
2 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
2 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,
1 g/^\*e\*.EQ/+,/^\*e\*.EN/-p
2 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 CommandsIt 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
2 is sufficient. The `\*e' 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 `\*e'. (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
1 g/x/s//y/\*e s/a/b/
2 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:
1 g/x/s/x/y/\*e s/a/b/
2
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 `\*e' at the end of each line except the last. Thus to add a `.nf' and `.sp' command before each `.EQ' line, type
1 g/^\*e\*.EQ/i\*e \*.nf\*e \*.sp
2 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.