LINUX Vim Replacement string instruction

Source: Internet
Author: User

Vi/vim can be used to replace a string with the: s command. Used to only use a format to replace the full text, today found that the command has a number of ways to write (vi is really powerful ah, there are many need to learn), several records in this, easy to query later.

: s/vivian/sky/replaces current line first Vivian for Sky

: S/vivian/sky/g replaces current line all Vivian for Sky

: N, $s/vivian/sky/replaces the first n line to the top Vivian for each line in the last line of Sky

: N, $s/vivian/sky/g replaces the first n line to each line in the last line all Vivian for Sky

N is a number, and if N is., it means starting from the current line to the last line

:%s/vivian/sky/(equivalent to: g/vivian/s//sky/) replaces the first Vivian of each line as Sky

:%s/vivian/sky/g (equivalent to: g/vivian/s//sky/g) replaces all Vivian in each row as Sky

You can use # as a separator, in which the middle appears/is not a separator

: s#vivian/#sky/# Replace the first vivian/of the current line as sky/

:%s+/oradata/apras/+/user01/apras1+ (using + to replace/):/oradata/apras/replaced with/user01/apras1/

1.:s/vivian/sky/replaces current line first Vivian for Sky

: S/vivian/sky/g replaces current line all Vivian for Sky

2.: N, $s/vivian/sky/replaces the first n line to the top of each line in the last line Vivian for Sky

: N, $s/vivian/sky/g replaces the first n line to each line in the last line all Vivian for Sky

(n is a number, if N is., which means starting from the current line to the last line)

3.:%s/vivian/sky/(equivalent to: g/vivian/s//sky/) replaces the first Vivian of each line for Sky

:%s/vivian/sky/g (equivalent to: g/vivian/s//sky/g) replaces all Vivian in each row as Sky

4. You can use # as a separator, which appears in the middle/not as a separator

: s#vivian/#sky/# Replace the first vivian/of the current line as sky/

5. Delete the ^m in the text

Problem Description: For newline, window with carriage return Line (0A0D) to express, Linux is a carriage return (0A) to express. This way, there will always be a ^m when you copy files on Windows to UNIX. Write a shell or C program that filters a line break (0D) for a window file under UNIX.

。 Using commands: Cat filename1 | tr-d "^v^m" > NewFile;

。 Use command: sed-e "s/^v^m//" filename > outputfilename. Note that in 1, 22 of the methods, ^v and ^m refer to Ctrl + V and ctrl+m. You have to manually input, not paste.

。 In VI Processing: First use VI to open the file, and then press the ESC key, 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 "Dest"

。 Strings A>b

6. Other

Use the: s command to implement a string substitution. The specific uses include:

: s/str1/str2/replaces the first occurrence of a string with a string str2 str1

: S/str1/str2/g replaces all occurrences of the string in a line with a string str2 str1

:。 , $ s/str1/str2/g Replaces all occurrences of the body's current line to the end with a string str2 str1

: 1,$ s/str1/str2/g replaces all occurrences of a string in the body with a string str2 str1

: g/str1/s//str2/g function ditto

From the above substitution command you can see that G is placed at the end of the command to replace each occurrence of the search string;

The first occurrence of the string is substituted; G is placed at the beginning of the command to replace all the lines in the body that contain the search string.

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.