Linux Vi replacement character command operation instance

Source: Internet
Author: User
Tags expression engine

Linux Vi replacement character command operation instance

In Linux development, it is often involved in processing the characters in the file, and replacement of the characters is also very frequent.

This document uses an actual file as an example to describe the commonly used vi replacement character commands in Linux, which provides reference for related development work.

 

Vim Learning Guide

Quick learn Vi Editor

Powerful Vim Editor

Build a Vim Development Environment on CentOS 6.2

Vim 7.4a released, a new and faster Regular Expression Engine

Install the highlighted Vim editing tool in CentOS 5.4

Vim tips: C language settings

In this document, the operated file is testfile.txt, And the content is as follows:

Www.bkjia.com @ linux :~ /Bkjia/Test> cat TestFile.txt

12345678907890

ABCDABCDEFGHIJ

12345 ^ &*()()

98765432103210

Abcdabcdefghij123123

 

Enter the vi editing mode first, as shown below:

Www.bkjia.com @ linux :~ /Bkjia/Test> vi TestFile.txt

 

Run the following replace command in sequence (Note: Each Command must start with the colon ":"), and the replace part is marked in red.

1): n, $ s/str1/str2/

Replace the first str1 in each row from row n to row 2.

For example, run the following command in the vi editing mode of the testfile.txt file:

: 2, $ s/123/321/

The file content is changed:

12345678907890

ABCDABCDEFGHIJ

32145 ^ &*()()

98765432103210

Abcdabcdefghij321123

It can be seen that only the first "123" of the third row and the fifth row is changed to "321 ". "123" in the first line is not replaced because the command is executed from the second line.

 

2): n, $ s/str1/str2/g

Replace all str1 in each row from line n to the last row with str2.

Then run the following command:

: 2, $ s/3210/1234/g

The file content is changed:

12345678907890

ABCDABCDEFGHIJ

32145 ^ &*()()

98765412341234

Abcdabcdefghij321123

It can be seen that all "3210" starting from the second line is changed to "1234 ".

 

3): % s/str1/str2/

Replace the first str1 in each row with str2.

Then run the following command:

: % S/123/321/

The file content is changed:

32145678907890

ABCDABCDEFGHIJ

32145 ^ &*()()

98765432141234

Abcdabcdefghij321321

It can be seen that the first "123" in each row is changed to "321 ".

 

4): % s/str1/str2/g

Replace all str1 in each row with str2.

Then run the following command:

: % S/890/098/g

The file content is changed:

32145670987098

ABCDABCDEFGHIJ

32145 ^ &*()()

98765432141234

Abcdabcdefghij321321

It can be seen that all "890" in each row is changed to "098 ".

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.