2011-06-16 wcdj
(1) Confirm replacement
(2) Context-related replacement
(3) pattern matching rules
(1) usage of metacharacters in Search Mode
(2) Some pattern matching examples
Global replacement actually uses two ex commands -- g (global) and S (replacement)
Replace command syntax
As follows:
(1)
: S/old/new/
This will change the first appearance of the mode old in the current row to new
/(Slash) is the delimiter between different parts of the command (it is optional when the slash is the last character of the line ).
(2)
: S/old/new/g
Change the appearance of the old row to new instead of the first old row.
The: s command allows replacement of the following options. The G option in the above syntax represents the global (Note: The G option affects each mode in a row and does not affect all rows in the file: g command obfuscation ).
(3)
50,100 S/old/new/g
You can add an address prefix before the: s command to extend its range to multiple lines.
: 1, $ S/old/new/g
Change the appearance of old in the entire file to new
: % S/old/new/g
You can also use % to replace 1, $ to specify each row in the file.
Global replacement is much faster than searching for and replacing strings for each instance. This command can be modified for different types and has powerful functions.
(1) Confirm replacement
The search replacement command must be very careful, and sometimes the result is not desired. You can enterU
To cancel any search replacement command, provided that the command is the last edit operation. However, it is not always possible to find unexpected modifications when they can be recovered. Another way to protect the edited file is to use W to save the file before global replacement. In this way, you can exit the file without saving and editing, and then return the status before the file is modified. You can also use: E! Read the previous version of the buffer.
If you want to see the search result and confirm each replacement before replacement, you can addC option
(For confirmation ):
: 1, 30 s/his/The/GC
The replacement will be confirmed one by one. If you want to replace the string, you must enter y (indicating yes) and press the return key. If you do not want to replace the string, you only need to press return. (??? Try and find that the installed version is different. When replacing, Press Y. If it is not replaced, press n. You do not need to press return)
Note:
N (repeat the previous search) and vertex (.) of vi (.) (repeat the previous command) the combination of commands is also a very useful method, it can quickly flip pages in the file to execute modifications that do not want to replace all.
For example, if you use which when you use that, you can check the appearance of which and only modify the incorrect ones:
/Which search which
Cwthat + ESC to that
N repeated searches, skipping one modification
N repeated searches, skipping one modification
. Change to that and use the. Command to repeat the modification.
......
(2) Context-related replacement
The simplest global replacement is to use one word (or phrase) to replace another. There are also a slightly more complex global replacement syntax, which can be used to search for a pattern. Once a row containing the pattern is found, it can be replaced with strings different from the pattern, consider this replacement as context-related replacement.
: G/pattern/S/old/new/g
The first G is the command executed on all lines of the file. Pattern identifies the line to be replaced. On the rows that contain pattern, ex will replace (s) with new. The last G indicates that all replace is performed on this row.
If the pattern used for search is the same as the pattern to be modified, you do not need to repeat it. For example:
: G/string/S // new/g is equivalent
: G s/string/New/g is equivalent
: % S/string/New/g
Search for rows containing strings and replace string with new.
(3) pattern matching rules
During global replacement, the Unix Editor similar to VI allows you to search for variable word modes that are not just fixed strings, but also referred to by regular expressions.
Regular Expressions are composed of common characters and many special characters called metacharacters.
(1) usage of metacharacters in Search Mode
.
Match any single character except line breaks
*
Matches zero or multiple instances with a single character before it
^
Start with what, when ^ does not start with a regular expression, ^ only represents itself
$
End with a regular expression. When $ does not end with a regular expression, $ only indicates itself.
/
The following special characters are treated as common characters, and/(backslash) prevents the interpretation of special characters. For example,/. matches with a real vertex instead of "any single character"
[]
Match any of the characters in square brackets. For example, [AB] matches A or B.
Note:
(1) You can use a hyphen to separate the first and last characters in the range to specify a consecutive character range. For example, [A-Z] will match any uppercase letter between A and Z, and [0-9] will match any number between 0 and 9.
(2) You can include multiple ranges in square brackets or combine the ranges with individual characters. For example, [:; A-Za-z ()] will match four different punctuation marks and all letters.
(3) Most metacharacters will lose their special meanings in square brackets, so they do not need to be escaped. in square brackets, the three metacharacters to be escaped are //,-, and].
(4) The insertion symbol (^) has a special meaning only when it is the first character in square brackets, but in this case its meaning is different from that of the common ^ metacharacters. As the first character of square brackets, ^ reversed their meaning: square brackets match any character not in the list. For example, [^ A-Z] will match any character that is not a lowercase letter.
/(/)
Save the mode between/(and/) to a dedicated bucket or a "storage buffer. In this way, up to nine modes can be saved to a single row. For example:
/(That/) or/(This/) Save that to the No. 1 storage buffer and save this to the No. 2 storage buffer. You can use sequence/1 to/9 in replacement to re-reference the storage mode.
: % S // (that/) or/(This/) // 2 or/1/change this or that to that or this
/</>
Matches the START (/<) or end (/>) of a word. The end or start of a word is determined by punctuation marks or spaces.
Note: Unlike/(.../), these commands do not need to be used in pairs.
~
Matches any regular expression used in the previous search. (Rarely used)
(2) Some pattern matching examples
: % S /// home // Gerry /// home // wcdj/g to modify the path name list in the file. Note: escape and use // to get it/
: 1, 10 S //./;/g, change all the dots in line 1 to 10 to semicolons. Note: Escape
: % S/[HH] ELP/help/g. Change all help (or help) words to help.
: % S/[HH] ELP // U &/G is the same as above./u converts all the modes after it into uppercase. When & is used for replacement, it indicates the entire text that matches the search mode.
: G/^ $/d delete all empty rows, and the end of the line ($) is followed by the beginning of the line (^)