In VI/vim, you can use the: s command to replace the string. This command can be used in many different ways to implement complex functions.
: S/Vivian/sky/
Replace the first Vivian in the current row with sky
: S/Vivian/sky/g
Replace all Vivian of the current row with sky
: N, $ S/Vivian/sky/
Replace the first Vivian from line N to line N with Sky.
: N, $ S/Vivian/sky/g
Replace all Vivian values from row n to row N as sky
N is a number. If n is., it indicates starting from the current row to the last row.
: % S/Vivian/sky/
: G/Vivian/S // sky/
Replace the first Vivian of each row with sky
: % S/Vivian/sky/g
: G/Vivian/S // sky/g
Replace all Vivian in each row with sky
You can use # As the separator. The/in the middle will not be used as the separator.
: S # Vivian/# Sky/# Replace the first Vivian/in the current line with Sky/
: % S +/oradata/apras/+/user01/apras1 + (replace with +/):/oradata/apras/Replace with/user01/apras1/
1.: S/Vivian/sky/Replace the first Vivian in the current row with sky
: S/Vivian/sky/g replace all Vivian in the current row with sky
2.: N, $ S/Vivian/sky/Replace the first Vivian from row n to row N with sky
: N, $ S/Vivian/sky/g replace all Vivian values from row n to row n
(N is a number. If n is., it indicates starting from the current row to the last row)
3. Replace % S/Vivian/sky/(equivalent to: G/Vivian/S // sky/) with the first Vivian of each row as sky
: % S/Vivian/sky/g (equivalent to: G/Vivian/S // sky/g) replace all
4. You can use # As the separator. The/in the middle will not be used as the separator.
: S # Vivian/# Sky/# Replace the first Vivian/in the current line with Sky/
5. Delete ^ m from the text
Problem description: For line breaks, use the carriage return line break (0a0d) in the window, and the carriage return (0a) in Linux. In this way, when you copy files on Windows to Unix, there will always be a ^ m. Please write a shell or C that is used in UNIX to filter Windows files (0d)Program.
. Run the command: CAT filename1 | tr-d "^ V ^ m"> newfile;
. Run the following command: sed-E "S/^ V ^ m //" FILENAME> outputfilename. in methods 1 and 2, ^ V and ^ m indicate Ctrl + V and CTRL + M. you must enter the file manually instead of pasting it.
. Processing in VI: first use VI to open the file, then press ESC, and then enter the command: % S/^ V ^ m //.
. : % S/^ m $/g
If the above method is useless, the correct solution is: [Page]
. Tr-d \ "\ r \" <SRC> dest
. Tr-d \ "\ 015 \" dest
. Strings A> B
6. Confirm replacement
We usually need a character (string) inArticleSome locations are replaced when they appear, while other locations are not replaced. This requires confirmation by the user. VI search replacement is also supported.
For example
: S/Vivian/sky/g replace all Vivian in the current row with sky
You can add a letter C after the command, that is, S/Vivian/sky/GC.
Confirm stands for confirm.
7. Others
Use the S command to replace strings. The specific usage includes:
: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
:., $ S/str1/str2/g replace string str1 with string str2 from the current row to the end of the body
: 1, $ S/str1/str2/g replace str1 with string str2
: G/str1/S // str2/g functions are the same as above
From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search
String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.