"The Art of substitution in VI"-linux command five-minute series Xi.

Source: Internet
Author: User

VI aspects of the content do not know where to classify the good, put it in the "Linux command five-minute series" In it!

Today programming, a small example of the stack, during which I need to put "S." Replace with "s->" (the substitution does not include double quotes).

In fact, this is not difficult, but I think we should summarize the VI in the replacement technology, for later review.

1

All alternatives are written in the colon ":" state.

2

If you want to replace ABC with XYZ, that's it.

: s/abc/xyz/

However, it is important to note that this substitution is limited to the current line of the cursor, and, worse, it replaces only the first occurrence of the current row.

That is, if the line content is I write ABC, I read XYZ and ABC. Then the result of the substitution is I write xyz, I read XYZ and ABC.

3

If you want to replace all the matches in a row, this is the case:

: s/abc/xyz/g

4

If you want to put all of the "S." In a row Replace with "s->", then this is the case:

: s/s\./s->/g

For example, this line s.abc and S->BCD and S.aaa and S. ABC for, replaced by S->ABC and S->BCD and S->aaa and S->.ABC

Here because of the dot "." is a special match, so if you want to match the point number, you need to put it in front of the dot number, indicating the special meaning of the cancellation point number.

5

If you want to widen the replacement range, you can use the number of rows to limit:

If you want to replace the full-text of S. for S->, then:

: 1, $s/s\./s->/g

It can be seen that adding m,n in front of S indicates the range of rows, 1 represents the first row, and $ represents the last row. Of course you can specify a different number of rows.

Here, M or n can be used with special symbols "." Or "$" stands for, "." Represents the current line, and "$" represents the last line

If you are sure to replace the full text, you can replace 1,$ with%, that is:%s/s\./s->/g

6

You may find it troublesome if I want to match a http://roclinux.cn/index.php because//symbols and/or symbols will confuse replacements. At this point, you can completely replace with # or any other symbol, such as s/abc/xyz/can be replaced with s#abc#xyz#, or s!abc!xyz! is possible! It's not magical, and if you know Perl, you'll find more very, very flexible tricks. Oh

This line of content is: http://roclinux.cn/index.php and HTTP:/UUW/FAF

Use the following replacement command (# used here):

: s#http://roclinux\.cn/index\.php#http://www\.sohu\.com#

Replaced by: Http://www.sohu.com and HTTP:/UUW/FAF

7

You can do this if I want to add a pair of single quotes to both sides of the string with the date format (XXXX-XX-XX), that is, 2008-07-01 becomes ' 2008-07-01 '.

For example, the original file content is:

AAA 2008-07-01 BBB
CCC 2008-07-12 AAA

Use the following replacement command:

: 1, $s/\ ([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)/' \1 '/g

Replaced by:

AAA ' 2008-07-01′BBB
CCC ' 2008-07-12′AAA

This replacement command is more complex, with these points of knowledge:

First knowledge point: [0-9] represents any number between 0 and 9

The second point of Knowledge: \{n\} means that the elements preceding this symbol are repeated n times, so [0-9]\{4\} represents a four-digit number

The third point of knowledge: if you use \ (\) in the matching scheme to indicate that you want to stage the match, and \1 to replace it with a staged match, you can understand that if we match the 2008-07-01, then the parentheses will be staged, and the following \ 1 places will be replaced, the outside with a pair of quotation marks, OK!

"The Art of substitution in VI"-linux command five-minute series Xi.

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.