Annotation of 43BSDReno/share/doc/usd/12.edtut/e4, revision 1.1.1.1

1.1       root        1: .\"    @(#)e4  6.1 (Berkeley) 5/22/86
                      2: .\"
                      3: .SH
                      4: Exercise 5:
                      5: .PP
                      6: Experiment with the substitute command.
                      7: See what happens if you
                      8: substitute for some word on a line with several occurrences of that word.
                      9: For example, do this:
                     10: .P1
                     11: a
                     12: the other side of the coin
                     13: \*.
                     14: s/the/on the/p
                     15: .P2
                     16: You will get
                     17: .P1
                     18: on the other side of the coin
                     19: .P2
                     20: A substitute command changes only the first occurrence of the first string.
                     21: You can change all occurrences by adding a
                     22: .UL g
                     23: (for ``global'')
                     24: to the
                     25: .UL s
                     26: command, like this:
                     27: .P1
                     28: s/ . . . / . . . /gp
                     29: .P2
                     30: Try other characters instead of slashes to delimit the two sets
                     31: of characters in the
                     32: .UL s
                     33: command \- anything should work
                     34: except blanks or tabs.
                     35: .PP
                     36: (If you get funny results using any of the characters
                     37: .P1
                     38: ^    \*.    $    [    *    \e    &
                     39: .P2
                     40: read the section on ``Special Characters''.)
                     41: .SH
                     42: Context searching \- ``/ . . . /''
                     43: .PP
                     44: With the substitute command mastered, you can move on to
                     45: another highly important idea of
                     46: .ul
                     47: ed
                     48: \- context searching.
                     49: .PP
                     50: Suppose you have the original three line text in the buffer:
                     51: .P1
                     52: Now is the time
                     53: for all good men
                     54: to come to the aid of their party.
                     55: .P2
                     56: Suppose you want to find the line that contains
                     57: .IT their
                     58: so
                     59: you can change it to
                     60: .IT the .
                     61: Now with only three lines in the buffer, it's pretty easy
                     62: to keep track of what line the word
                     63: .IT their
                     64: is on.
                     65: But if the buffer contained several hundred lines,
                     66: and you'd been making changes, deleting and rearranging lines,
                     67: and so on, you would no longer really know what this line
                     68: number would be.
                     69: Context searching is simply a method of specifying the desired line,
                     70: regardless of what its number is,
                     71: by specifying some context on it.
                     72: .PP
                     73: The way to say ``search for a line
                     74: that contains this particular string of characters''
                     75: is to type
                     76: .P1
                     77: /\fIstring of characters we want to find\fP/
                     78: .P2
                     79: For example,
                     80: the
                     81: .ul
                     82: ed
                     83: command
                     84: .P1
                     85: /their/
                     86: .P2
                     87: is a context search which
                     88: is sufficient to find the desired line \-
                     89: it will locate the next occurrence of
                     90: the characters between slashes (``their'').
                     91: It also sets dot to that line
                     92: and prints the line for verification:
                     93: .P1
                     94: to come to the aid of their party.
                     95: .P2
                     96: ``Next occurrence'' means that
                     97: .ul
                     98: ed
                     99: starts looking for the string at line
                    100: .UL .+1 ,
                    101: searches to the end of the buffer,
                    102: then continues at line 1 and searches to line dot.
                    103: (That is, the search ``wraps around'' from
                    104: .UL $
                    105: to
                    106: 1.)
                    107: It scans all the lines in the buffer until it either finds the desired line
                    108: or gets back to dot again.
                    109: If the given string of characters can't be found in any line,
                    110: .ul
                    111: ed
                    112: types the error message
                    113: .P1
                    114: ?
                    115: .P2
                    116: Otherwise it prints the line it found.
                    117: .PP
                    118: You can do both the search for the desired line
                    119: .ul
                    120: and
                    121: a
                    122: substitution all at once, like this:
                    123: .P1
                    124: /their/s/their/the/p
                    125: .P2
                    126: which will yield
                    127: .P1
                    128: to come to the aid of the party.
                    129: .P2
                    130: There were three parts to that last command:
                    131: context search for the desired line, make the substitution, print the line.
                    132: .PP
                    133: The expression
                    134: .UL /their/
                    135: is a context search expression.
                    136: In their simplest form,
                    137: all context search expressions are like this \-
                    138: a string of characters surrounded by slashes.
                    139: Context searches are interchangeable with line numbers,
                    140: so they can be used by themselves to find and print a desired line,
                    141: or as line numbers for some other command, like
                    142: .UL s .
                    143: They were used both ways in the examples above.
                    144: .PP
                    145: Suppose the buffer contains the three familiar lines
                    146: .P1
                    147: Now is the time
                    148: for all good men
                    149: to come to the aid of their party.
                    150: .P2
                    151: Then the
                    152: .ul
                    153: ed
                    154: line numbers
                    155: .P1
                    156: /Now/+1
                    157: /good/
                    158: /party/\-1
                    159: .P2
                    160: are all context search expressions, and they all refer
                    161: to the same line (line 2).
                    162: To make a change in line 2,
                    163: you could say
                    164: .P1
                    165: /Now/+1s/good/bad/
                    166: .P2
                    167: or
                    168: .P1
                    169: /good/s/good/bad/
                    170: .P2
                    171: or
                    172: .P1
                    173: /party/\-1s/good/bad/
                    174: .P2
                    175: The choice is dictated only by convenience.
                    176: You could print all three lines by, for instance
                    177: .P1
                    178: /Now/,/party/p
                    179: .P2
                    180: or
                    181: .P1
                    182: /Now/,/Now/+2p
                    183: .P2
                    184: or by any number of similar combinations.
                    185: The first one of these might be better if you don't
                    186: know how many lines are involved.
                    187: (Of course, if there were only three lines in the buffer,
                    188: you'd use
                    189: .P1
                    190: 1,$p
                    191: .P2
                    192: but not if there were several hundred.)
                    193: .PP
                    194: The basic rule is: a context search expression is
                    195: .ul
                    196: the same as
                    197: a line number, so it can be used wherever a line number is needed.
                    198: .SH
                    199: Exercise 6:
                    200: .PP
                    201: Experiment with context searching.
                    202: Try a body of text with
                    203: several occurrences
                    204: of the same string of characters, and scan through it using
                    205: the same context search.
                    206: .PP
                    207: Try using context searches as line numbers for the
                    208: substitute, print and delete commands.
                    209: (They can also be used
                    210: with
                    211: .UL r ,
                    212: .UL w ,
                    213: and
                    214: .UL a .)
                    215: .PP
                    216: Try context searching using
                    217: .UL ?text?
                    218: instead of
                    219: .UL /text/ .
                    220: This scans lines in the buffer in reverse order
                    221: rather than normal.
                    222: This is
                    223: sometimes useful if you go too far while looking for some
                    224: string of characters \- it's an easy way to back up.
                    225: .PP
                    226: (If you get funny results with any of the characters
                    227: .P1
                    228: ^    \*.    $    [    *    \e    &
                    229: .P2
                    230: read the section on ``Special Characters''.)
                    231: .PP
                    232: .ul
                    233: Ed
                    234: provides a shorthand for repeating a context search
                    235: for the same string.
                    236: For example,
                    237: the
                    238: .ul
                    239: ed
                    240: line number
                    241: .P1
                    242: /string/
                    243: .P2
                    244: will find the next occurrence of
                    245: .UL string .
                    246: It often happens that this is not the desired line,
                    247: so the search must be repeated.
                    248: This can be done by typing merely
                    249: .P1
                    250: //
                    251: .P2
                    252: This shorthand stands for ``the most recently used
                    253: context search expression.''
                    254: It can
                    255: also be used as the first string of the substitute
                    256: command, as in
                    257: .P1
                    258: /string1/s//string2/
                    259: .P2
                    260: which will find the next occurrence of
                    261: .UL string1
                    262: and replace it by
                    263: .UL string2 .
                    264: This can save a lot of typing.
                    265: Similarly
                    266: .P1
                    267: ??
                    268: .P2
                    269: means ``scan backwards for the same expression.''

unix.superglobalmegacorp.com

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