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