Linux Base command: Find and Replace in VI mode

Source: Internet
Author: User

First, find
Find command

/pattern<enter>: Find pattern Match string down
Pattern<enter>: Find pattern Match string up
After using the Find command, use the following two keys to quickly find:
N: Continue searching in the same direction
N: Search in the opposite direction
Pattern is a string that needs to be matched, for example:
/hello<enter> #查找hello
/hello<enter> #查找hello单词 (note the space before and after)
In addition, pattern can also use some special characters, including (/, ^, $, *,.), of which the first three of these two are VI and Vim universal, "/" for the escape character.
/^hello<enter> #查找以hello开始的行
/hello$<enter> #查找以hello结束的行
^hello<enter> #查找 ^hello String
Second, replace
1.:[range]s/pattern/string/[c,e,g,i]

Range refers to the range, 1,7 means from the first line to the seventh line, 1,$ means from the first line to the last row, that is, the entire article, can also be a% representative. % is the currently edited article, # is the previous edit article .
Pattern is the string to be replaced, which can be represented by RegExp.
String replaces the pattern with string.
c Confirm, will be asked before each replacement.
e does not show error.
G Globe, do not ask, the whole line is replaced.
I ignore is not case-sensitive.
I Ignore is case sensitive.
2. Basic Replacement
: s/lantian/sky/#替换当前行第一个 Lantian for Sky
: s/lantian/sky/g #替换当前行所有 Lantian for Sky
: N, $s/lantian/sky/#替换第 the first Lantian of each row in line n start to the last row is sky
: N, $s/lantian/sky/g #替换第 n lines start to the last row all Lantian for each row are sky
# (n is a number, if N is., representing the beginning of the current line to the last row)
:%s/lantian/sky/# (equivalent to: g/lantian/s//sky/) ReplaceThe first Lantian of each row is sky
:%s/lantian/sky/g # (equivalent to: g/lantian/s//sky/g) replaces all Lantian in each row as Sky
You can use #或 + as a delimiter, in which case the middle/no delimiter
: s#lantian/#sky/# Replace the current row the first lantian/is sky/
:%s+/oradata/apras/+/user01/apras1+ (
Use + to replace/):/oradata/apras/Replace with/user01/apras1/

3. Delete the ^m in the text
Problem description: For line breaks, the window is replaced with a carriage return (0a0d) to indicate that Linux is a carriage return (0A) to represent. That way, when you copy a file on a window to UNIX, there's always a ^m, write a shell or C program that uses a newline character (0D) that filters Windows files under UNIX.
Using the command: Cat filename1 | tr-d "^v^m" > NewFile;
Use command: sed-e "s/^v^m//" filename > OutputFileName
It is important to note that in 1, 22 methods, ^v and ^m refer to Ctrl + V and ctrl+m. You have to do the input manually, not paste it.
Process in VI: First use VI to open the file, then press ESC, then enter the command:
:%s/^v^m//
:%s/^m$//g
If the above method is useless, the correct solution is:
Tr-d "/R" < src >dest
Tr-d "/015″dest
Strings A>b

4. Other uses
: s/str1/str2/#用字符串 str2 The first occurrence of the string in the replacement row str1
: s/str1/str2/g #用字符串 str2 Replace all occurrences of the string in the row str1
:., $ s/str1/str2/g #用字符串 str2 Replaces all occurrences of the text current line to the end of the string str1
: 1,$ s/str1/str2/g #用字符串 str2 Replace all occurrences of the string in the body str1
: g/str1/s//str2/g #功能同上

Summary description of 5.GFrom the Replace command above you can see:
The G is placed at the end of the command to replace each occurrence of the search string for the specified row, without G, which means that only the first occurrence of the search string for the specified row is replaced;
The G is placed at the beginning of the command to replace all rows in the body that contain the search string.
That is, the start of the command can add rows that affect, and if G represents all rows, the end of the command can use G to indicate whether all strings in each row are affected.

Linux Base command: Find and Replace in VI mode

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.