|
|
1.1 ! root 1: .\" @(#)e6 6.1 (Berkeley) 5/22/86 ! 2: .\" ! 3: .SH ! 4: Special Characters ! 5: .PP ! 6: You may have noticed that things just don't work right when you used ! 7: some characters like ! 8: \*., ! 9: .UL * , ! 10: .UL $ , ! 11: and others in ! 12: context searches and the substitute command. ! 13: The reason is rather complex, although the cure is simple. ! 14: Basically, ! 15: .ul ! 16: ed ! 17: treats these characters as special, with special meanings. ! 18: For instance, ! 19: .ul ! 20: in a context search or the first string of the substitute command only, ! 21: \*. ! 22: means ``any character,'' not a period, so ! 23: .P1 ! 24: /x\*.y/ ! 25: .P2 ! 26: means ``a line with an ! 27: .UL x , ! 28: .ul ! 29: any character, ! 30: and a ! 31: .UL y ,'' ! 32: .ul ! 33: not ! 34: just ``a line with an ! 35: .UL x , ! 36: a period, and a ! 37: .UL y .'' ! 38: A complete list of the special characters ! 39: that can cause trouble is the following: ! 40: .P1 ! 41: ^ \*. $ [ * \e ! 42: .P2 ! 43: .ul ! 44: Warning: ! 45: The backslash character ! 46: .UL \e ! 47: is special to ! 48: .ul ! 49: ed. ! 50: For safety's sake, ! 51: avoid it where possible. ! 52: If you have to use one of the special characters ! 53: in a substitute command, ! 54: you can turn off its magic meaning temporarily ! 55: by preceding it with the backslash. ! 56: Thus ! 57: .P1 ! 58: s/\e\e\e\*.\e*/backslash dot star/ ! 59: .P2 ! 60: will change ! 61: .UL \e.* ! 62: into ``backslash dot star''. ! 63: .PP ! 64: Here is a hurried synopsis of the other special characters. ! 65: First, the circumflex ! 66: .UL ^ ! 67: signifies ! 68: the beginning of a line. ! 69: Thus ! 70: .P1 ! 71: /^string/ ! 72: .P2 ! 73: finds ! 74: .UL string ! 75: only if it is at the beginning of a line: ! 76: it will find ! 77: .P1 ! 78: string ! 79: .P2 ! 80: but not ! 81: .P1 ! 82: the string... ! 83: .P2 ! 84: The dollar-sign ! 85: .UL $ ! 86: is just the opposite of the circumflex; ! 87: it means the end of a line: ! 88: .P1 ! 89: /string$/ ! 90: .P2 ! 91: will only find an occurrence of ! 92: .UL string ! 93: that is at the end of some line. ! 94: This implies, of course, ! 95: that ! 96: .P1 ! 97: /^string$/ ! 98: .P2 ! 99: will find only a line that contains just ! 100: .UL string , ! 101: and ! 102: .P1 ! 103: /^\*.$/ ! 104: .P2 ! 105: finds a line containing exactly one character. ! 106: .PP ! 107: The character ! 108: .UL . , ! 109: as we mentioned above, ! 110: matches anything; ! 111: .P1 ! 112: /x\*.y/ ! 113: .P2 ! 114: matches any of ! 115: .P1 ! 116: x+y ! 117: x-y ! 118: x y ! 119: x\*.y ! 120: .P2 ! 121: This is useful in conjunction with ! 122: .UL * , ! 123: which is a repetition character; ! 124: .UL a* ! 125: is a shorthand for ``any number of ! 126: .UL a 's,'' ! 127: so ! 128: .UL .* ! 129: matches any number of anythings. ! 130: This is used like this: ! 131: .P1 ! 132: s/\*.*/stuff/ ! 133: .P2 ! 134: which changes an entire line, ! 135: or ! 136: .P1 ! 137: s/\*.*,// ! 138: .P2 ! 139: which deletes all characters in the line up to and ! 140: including the last comma. ! 141: (Since ! 142: .UL .* ! 143: finds the longest possible match, ! 144: this goes up to the last comma.) ! 145: .PP ! 146: .UL [ ! 147: is used with ! 148: .UL ] ! 149: to form ``character classes''; ! 150: for example, ! 151: .P1 ! 152: /[0123456789]/ ! 153: .P2 ! 154: matches any single digit \- ! 155: any one of the characters inside the braces ! 156: will cause a match. ! 157: This can be abbreviated to ! 158: .UL [0\-9] . ! 159: .PP ! 160: Finally, the ! 161: .UL & ! 162: is another shorthand character \- ! 163: it is used only on the right-hand part of a substitute command ! 164: where it means ``whatever was matched on the left-hand side''. ! 165: It is used to save typing. ! 166: Suppose the current line contained ! 167: .P1 ! 168: Now is the time ! 169: .P2 ! 170: and you wanted to put parentheses around it. ! 171: You could just retype the line, but ! 172: this is tedious. ! 173: Or you could say ! 174: .P1 ! 175: s/^/(/ ! 176: s/$/)/ ! 177: .P2 ! 178: using your knowledge of ! 179: .UL ^ ! 180: and ! 181: .UL $ . ! 182: But the easiest way uses the ! 183: .UL & : ! 184: .P1 ! 185: s/\*.*/(&)/ ! 186: .P2 ! 187: This says ``match the whole line, and replace it ! 188: by itself surrounded by parentheses.'' ! 189: The ! 190: .UL & ! 191: can be used several times in a line; ! 192: consider ! 193: using ! 194: .P1 ! 195: s/\*.*/&? &!!/ ! 196: .P2 ! 197: to produce ! 198: .P1 ! 199: Now is the time? Now is the time!! ! 200: .P2 ! 201: .PP ! 202: You don't have to match the whole line, of course: ! 203: if the buffer contains ! 204: .P1 ! 205: the end of the world ! 206: .P2 ! 207: you could type ! 208: .P1 ! 209: /world/s//& is at hand/ ! 210: .P2 ! 211: to produce ! 212: .P1 ! 213: the end of the world is at hand ! 214: .P2 ! 215: Observe this expression carefully, ! 216: for it illustrates how to take advantage of ! 217: .ul ! 218: ed ! 219: to save typing. ! 220: The string ! 221: .UL /world/ ! 222: found the desired line; ! 223: the shorthand ! 224: .UL // ! 225: found the same ! 226: word in the line; ! 227: and the ! 228: .UL & ! 229: saves you from typing it again. ! 230: .PP ! 231: The ! 232: .UL & ! 233: is a special character only within ! 234: the replacement text of a substitute command, ! 235: and has no special meaning elsewhere. ! 236: You can turn off the special meaning of ! 237: .UL & ! 238: by preceding it with a ! 239: .UL \e : ! 240: .P1 ! 241: s/ampersand/\e&/ ! 242: .P2 ! 243: will convert the word ``ampersand'' into the literal symbol ! 244: .UL & ! 245: in the current line.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.