Replace string commands with LINUXvim

Source: Internet
Author: User
In vi/vim, you can use the: s command to replace the string. In the past, only one format was used to replace the full text. Today, we found that this command can be used to replace strings in many ways in vi/vim. In the past, we used only one format to replace the full text. today we found that there are many ways to write this command (vi is really powerful, and there are still a lot to learn). record several methods here to facilitate future queries.
 
: 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
 
: N, $ s/vivian/sky/replace the first vivian from row n to row n as 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.
 
: % S/vivian/sky/(equivalent to: g/vivian/s // sky/) replace the first vivian of each row with sky
 
: % S/vivian/sky/g (equivalent to: g/vivian/s // sky/g) replace all
 
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 program that is used in unix to filter windows files (0D.
 
. 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:
 
. Tr-d "\ r" dest
 
. Tr-d "\ 015" dest
 
. Strings A> B
 
6. 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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.