Word a substitution in Vim editor

Source: Internet
Author: User

This article is for the repost:

There are two methods:

(a) replaced by the VI editor.

Reference blog:

http://www.linuxdiyf.com/viewarticle.php?id=99843

http://kingaragorn.javaeye.com/blog/467308

You can use the: s command to replace a string in Vi/vim. Previously only use editing software to replace, today found that the command has many kinds of writing (vi is really powerful ah, there is a lot to learn), record several here, convenient later query.

: s/well/good/ replaces the current line with the first well being a good

: S/well/good/g replaces the current line all well is good

: N, $s/well/good/replaces the first well of each row in the beginning of the nth row to the last row good

: N, $s/well/good/g replace the nth line to the last row, and all well is good for each row

N is a number, if N is., indicating the beginning of the current line to the last row

:%s/well/good/(equivalent to: g/well/s//good/) replaces the first well of each row as good

:%s/well/good/g (equivalent to: g/well/s//good/g) replaces all the well in each row as good

You can use # as a delimiter, at which time the middle/not as a delimiter

: s#well/#good/# Replace the current row the first well/is good/

:%s#/usr/bin#/bin#g

You can change all the paths in the file/usr/bin to/bin.

(ii) The Sumly method directly replaces the string in the file. (This method replaces the string without opening the file, and can replace multiple files in bulk.) )

Reference post: http://easytech.javaeye.com/blog/316948

Example 1

Execute command:
Perl-p-i-e "s/china/sumly/g"/www/*.htm/www/*.txt

The above means that the "China" in all the HTM and txt files in the WWW folder is replaced with "sumly"

Example 2

Execute command:

Perl-p-i-e "s/memory_production/memory_production2/g"./*.sql

The above means replacing "memory_production" in all the SQL files in the current directory with "Memory_production2"

======================================
Linux SED Bulk replaces strings in multiple files
Http://www.svn8.com/linux/accidence/20100422/29910.html

Bulk Replace file content under Linux 1, on-line information on the network

Format: Sed-i "s/lookup field/replace field/g" ' grep lookup field-rl path '

SVN Chinese web

Linux SED Bulk replaces strings in multiple files

Sed-i "s/oldstring/newstring/g" ' grep oldstring-rl yourdir '

For example: Replace the www.admin99.net in all files under/home to Admin99.net Bbs.Svn8.Com

Sed-i "s/www.admin99.net/admin99.net/g" ' grep www.admin99.net-rl/home '

Bbs.Svn8.Com

Exp:sed-i "s/shabi/$/g" ' grep shabi-rl./' svn8.com

2. Additional svn8.com

2.1 Replace the text "garden" in file 1.txt with "Mirgarden"

# sed-i "s/garden/mirgarden/g" 1.txt//sed-i very simple

2.2 Replace "garden" in all files in the current directory with "Mirgarden"

# # Sed-i "s/garden/mirgarden/g" ' ls '//is actually the LS out of multiple filenames

====================================
How to replace text or strings in multiple files at the same time under Linux
Http://hi.baidu.com/jiqing0925/blog/item/0e97ea196662ab4242a9ad14.html
====================================
auspicious ([email protected])
Home: http://hi.baidu.com/jiqing0925
Reprint please retain the author information.
-------------------
There are often many ways to do something more complicated.
Replacing text in multiple files at once is a common problem, and here are three ways to solve this problem:

1. VI Method
See VIM User manual 26.
*26.3*Change multiple files

Suppose you have a variable named "x_cnt" and you want to change it to "X_counter".
This variable is used in multiple C files. You need to make this change in all files. You have to do this.
Put all relevant files into the parameter list:

:args *.c

This command will find all the C files and edit the first one.
Now you can perform the displace command on all these files:

:argdo %s/\<x_cnt\>/x_counter/ge | update

The command ": Argdo" treats another command as its argument. The latter will execute on all the files in the parameter list.
The Replace command "%s" as a parameter acts on all lines of text. It uses "\<x_cnt\>"To find" x_cnt ".
"\<" 和 "\>"is used to specify that only the complete words are matched, not" px_cnt "or" X_cnt2 ".

The markup for the Replace command contains "G", which replaces all occurrences of the matching word "x_cnt" within the same line of text.
The tag "E" is used to avoid error messages because "x_cnt" is not found in the file.
Otherwise, the ": Argdo" command will be interrupted when it encounters the first file that cannot find "x_cnt".
The character "|" separates two commands. The following "Update" command will save those files that have changed.
If no "x_cnt" is changed to "X_counter", this command does nothing.

There is also a ": Windo" command, which executes the commands specified by its parameters within all windows.
As well as the ": Bufdo" command, executes the commands specified by its parameters on all buffers.
Use caution because the number of files you have in the buffer list may be more than you can imagine.
Please use ": Buffers" command (or ": LS") to check the buffer list.

2. Perl Methods
Can be replaced at the command line,
The example in the VI method is still explained, and the "x_cnt" in C file is changed to "X_counter".
You can execute the following command:
find . -name ‘*.c‘ -print0 | xargs -0 perl -pi -e ‘s/x_cnt/x_counter/g‘

xargs 把find命令的结果作为perl的参数。
find的参数-print0和xargs的参数-0是防止文件名中有空格或新行造成错误,可以man xargs获得帮助。

Note: If the substituted characters include ()[]/"‘!? such special characters, you must precede the characters with a backslash \.

The advantage of this approach is that you don't have to start the editor quickly. The disadvantage is that unwanted substitutions are easy to make, such as replacing px_cnt with Px_counter.

3. Sed
It is still explained in the above example:
Find. -name "*.C"-print0 | xargs-0 sed-i ' s/x_cnt/x_counter/g '
The pros and cons are the same as Perl methods.

The difference between the above methods is only the use of tools, I believe there are other tools to do it.
Emacs, for example, can certainly do this, just because I don't have Emacs, so I'm not familiar with it.
I hope you will summarize your own handy method.

==================
Linux SED bulk replaces multiple strings

Http://www.itqun.net/content-detail/105074.html

For example, to modify the Zhangsan in all the files below the directory/modules to Lisi, do this:

Sed-i "s/zhangsan/lisi/g" ' grep zhangsan-rl/modules '

Explain:

-I means inplace edit, in-place modify file
-R indicates a search sub-directory
-l indicates output matching file name

This combination of commands is very powerful, pay attention to the backup file.


(1) Sed ' y/1234567890/abcdefghij/' test_sed

Sed ' y/1234567890/abcdefghij/' filename
Abcdefghij
Bcdefghija
Cdefghijab
Defghijabc
Note that the transformation relationship is based on the position of the two list.

Where: The content of test_sed is:

1234567890
2345678901
3456789012
4567890123


(2) Replace all matches per line

Sed ' s/01/ab/g ' test_sed
1234567890
23456789Ab
3456789ab2
456789ab23
Note: The first row of 0,1 is not replaced by A/b, respectively




Delete: D command

*

$ Sed ' 2d ' example-----Delete the second line of the example file.
*

$ sed ' 2, $d ' example-----Delete the second line of the example file to the end of all lines.
*

$ sed ' $d ' example-----Delete the last line of the example file.
*

$ sed '/test/' d example-----Delete all rows containing test in example file.

Replace: s command

*

$ sed ' s/test/mytest/g ' example-----Replace test with mytest in the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.
*

$ Sed-n ' s/^test/mytest/p ' example-----(-N) option and the P flag are used together to indicate that only those rows that have substitution occur are printed. That is, if the test at the beginning of a line is replaced with mytest, it is printed.
*

$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part that is found in the replacement string. All lines starting with 192.168.0.1 will be replaced with a self-added localhost, which becomes 192.168.0.1localhost.
*

$ Sed-n ' s/\ (love\) able/\1rs/p '

Word a substitution in Vim editor

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.