Annotation of 43BSDReno/share/doc/usd/13.edadv/ae4, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.