Vim string replacement command
:% s/str1/str2/g replaces all str1 in each line for str2 commonly used!
: s/str1/str2/g replaces all str1 in the current line to str2
Basic replacement
: s/str1/str2/ replaces the current line with str2 as the first str1
: s/str1/str2/g replaces all str1 in the current line to str2
The first str1 of each line from the beginning of line n to the last line of replacement is str2
All str1 in each line from the beginning of line n to the last line is str2
(n is a number, if n is., from the current line to the last line)
:% s/str1/str2/ (equivalent to: g/str1/s//str2/) replaces the first str1 of each line with str2
:% s/str1/str2/g (equivalent to: g/str1/s//str2/g) replaces all str1 in each line with str2
You can use # as the delimiter, and the / that appears in the middle will not be the delimiter
: s#str1/#str2/# replaces the current line with str2/ as the first str1/
:% swatches str11/str12/ str11 Universe str12 Universe Stern 22 + (use + to replace /): / str11/str12/ replace / str21/str22/
Delete ^ M from the file
Problem description: for line feeds, enter newline (0A0D) is used in Windows, and carriage return (0A) is used in Linux. In this way, when you copy a file on window to Unix, there will always be a ^ M. please write a shell or c program that filters the newline character (0D) of Windows file under Unix. There are two ways to do this:
(1) use the command: cat oldfilename | tr-d "^ V ^ M" > newfilename
(2) use the command: sed-e "s / ^ V ^ M / /" oldfilename > newfilename
It should be noted that in methods 1 and 2, ^ V and ^ M refer to Ctrl+V and Ctrl+M. You have to type it by hand, not paste it.
Processing in vim: first open the file using vim, then press the ESC key, and then enter the command:
:% s / ^ V ^ M / /
:% s / ^ M $/ / g
If the above methods are useless, the correct solution is:
(1) tr-d "\ r"
< old_file_name >New_file_name or cat oldfilename | tr-d "\ r" > new_file_name
(2) tr-d "\ 015"
< old_file_name >New_file_name or cat oldfilename | tr-d "\ 015" > new_file_name
Strings A > B
Other skills
You can use the: s command to replace a string. Specific uses include:
: s/str1/str2/ replaces the string str1 that first appears on the line with the string str2
S/str1/str2/g replaces all occurrences of the string str1 on the line with the string str2
:. $s/str1/str2/g replaces the string str1 that appears from the current line to the end of the body with the string str2
: 1 s/str1/str2/g $s/str1/str2/g replaces all the strings str1 that appear in the body with the string str1
G/str1/s//str2/g function is the same as above
From the above replacement command, you can see that g is placed at the end of the command, which means that each occurrence of the search string is replaced; without g, only the first occurrence of the search string is replaced; g is placed at the beginning of the command. means to replace all lines in the body that contain the search string.
The: s command can be used in vi/vim to replace strings. In the past, only one format was used to replace the full text. Today, I found that there are many ways to write this command (vi is really powerful, there is still a lot to learn). Record a few here to facilitate future query.
Replace
: s/vivian/sky/ replaces the current line with sky as the first vivian
: s/vivian/sky/g replaces all vivian in the current line to sky
The first vivian of each line from the beginning of line n to the last line of replacement is sky
All vivian in each line from the beginning of the nth line to the last line is sky
N is a number. If n is., it starts from the current line to the last line.
:% s/vivian/sky/ (equivalent to: g/vivian/s//sky/) replaces the first vivian of each line with sky
:% s/vivian/sky/g (equivalent to: g/vivian/s//sky/g) replaces all vivian in each line with sky
You can use # as the delimiter, and the / that appears in the middle will not be the delimiter
: s#vivian/#sky/# replaces the current line with sky/ as the first vivian/
:% oradata/apras/ oradata/apras/ aprasqpr apras1 + (replace with +): / replace with /): / replace with / apras1 +: / apras1 +
* *
Delete ^ M from text
Problem description: for line feeds, enter newline (0A0D) is used in window, and carriage return (0A) is used in linux. In this way, when you copy a file from window to unix, there will always be a ^ M. Please write a shell or c program to filter the newline character (0D) of windows files under unix.
Use the command: cat filename1 | tr-d "^ V ^ M" > newfile
Use the command: sed-e "s / ^ V ^ M / /" filename > outputfilename. It should be noted that in methods 1 and 2, ^ V and ^ M refer to Ctrl+V and Ctrl+M. You have to type it by hand, not paste it.
Processing in vi: first open the file using vi, then press the ESC key, and then enter the command:% s / ^ V ^ M / /.
:% s / ^ M $/ / g
If the above methods are useless, the correct solution is:
Tr-d "r" dest
Tr-d "5" dest
Strings A > B
Other
You can use the: s command to replace a string. Specific uses include:
: s/str1/str2/ replaces the string str1 that first appears on the line with the string str2
S/str1/str2/g replaces all occurrences of the string str1 on the line with the string str2
:. $s/str1/str2/g replaces the string str1 that appears from the current line to the end of the body with the string str2
: 1 s/str1/str2/g $s/str1/str2/g replaces all the strings str1 that appear in the body with the string str1
G/str1/s//str2/g function is the same as above
From the above replacement command, you can see that g is placed at the end of the command to replace each occurrence of the search string; without g, it means only the search
The first occurrence of the string is replaced; g is placed at the beginning of the command to replace all lines in the body that contain the search string.