Vi search and replacement

Source: Internet
Author: User
In Vi/vim, you can use the "s" command to replace the string. This command has many usage methods for different details, which can implement complex functions and record several types here for future query convenience.: S/vivian/sky/replace the first vivian in the current line with sk... Vi to find and replace the string. use the: s command in vi/vim to replace the string. This command has many usage methods for different details, which can implement complex functions and record several types here for future query convenience.: 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 with sky: n, $ s/vivian/sky/g replace all vivian from row n to row n. If n is ., 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) can be used as the separator to replace all vivian in each line with sky, in this case, the/in the middle will not be used as the separator: s # vivian/# sky/# replace the first vivian/in the current line Sky/: % s +/oradata/apras/+/user01/apras1 + (use + to replace/):/oradata/apras/with/user01/apras1/1.: s/vivian/sky/replace the first vivian of the current row with sky: s/vivian/sky/g, replace all vivian of the current row with sky 2.: n, $ s/vivian/sky/replace the first vivian from row n to row n as sky: n, $ s/vivian/sky/g replace all vivian from row n to row n with sky (n is a number, if n is ., indicates starting from the current row to the last row) 3.: % 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/vivi An/s // sky/g. you can use # as the separator. in this case, 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 in text description: for line breaks, use Carriage Return to line breaks (0A0D) in Windows, and press enter (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: [Page]. Tr-d \ "\ r \" <src> dest. Tr-d \ "\ 015 \" dest. Strings A> B 6. replacement confirmation we often need to replace a character (string) at some positions in the article, while other positions are not replaced with alternative operations, this requires confirmation by the user. the vi search replacement is also supported, for example: s/vivian/sky/g replace all vivian in the current line with sky, and add a letter c to the end of the command, that is, s/vivian/sky/gc, c is the abbreviation of confirm 7. other use: the s command can be used to replace strings. The specific usage includes: s/str1/str2/replace str1 that appears for the first time in the line with string str2: s/str1/str2/g replace string str1: with string str2 :., $ S/str1/str2/g replace str1: 1 with str2, $ s/str1/str2/g replace str1: g/str1/s // str2/g with string str2: g is placed at the end of the command to replace each appearance of the search string. without g, it means to replace the first appearance of the search string. g is placed at the beginning of the command, replaces all rows in the body that contain search strings.
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.