UNIX / Linux: vi / vim perform search and replace operation

2010-12-03  张林 

VI search and replace command format 总是记不住,写在这儿方便找

Simple format is as follows:
%s/old-string/new-string/

VI search and replace command examples

Let us say you would like to find a word called "foo" and replace with "bar".

First hit [Esc] key

Type : (colon) followed by %s/foo/bar/ and hit [Enter] key.
:%s/foo/bar/
Above command will replace first occurrence of word foo with bar on all lines. The % is shorthand for all lines.

To replace all occurrences of word foo with bar on all lines, use the g option (which indicates all occurrences on a line).
:%s/foo/bar/g

Note that the g can be replaced with a number 1,2,...N to change only the n'th occurrence on each line.

Use find and replace on line ranges (match by line numbers)

You can also make changes on range of lines i.e. replace first occurrence of foo with bar on lines 5 through 20 only, enter:
:5,20s/foo/bar/

Following command will replace first occurrence of foo with bar starting at the current line for the next 100 lines:
:.,+100s/foo/bar/

Match by words

Finally, you can match by words i.e. replace first occurrence of foo with bar starting at at the next line containing a word "test":
:/test/s/foo/bar/g
As usual you can specify ranges:
:/test/,/guest/s/foo/bar/g

Please note that all search/replace commands should be start with the [ESC]: keystroke combination only.

chgrp 用户组 文件或文件夹
chown 用户名 要改变的文件名
chmod u+x 文件名
chmod u-x 功能相反

Please following following step change ip while you first start the VM pc,

config virtual pc IP and name

vi /etc/sysconfig/network

 

NETWORKING=yes

HOSTNAME=ydcvm0xx

GATEWAY=172.18.133.5 # fixed network gateway

 

vi /etc/hosts

172.0.0.1               localhost.localdomain localhost

172.18.133.xxx      ydcvm0xx

 

/etc/sysconfig/network-scripts/ifcfg-eth0

chgrp ngenplay ngenplay
chown ngenplay ngenplay

[ngensimu@ydcvml03 ~]$ ls -l /backup/
total 32
drwxrwxr-x 2 ngenplay ngenplay  4096 2010-12-13 17:12 as
drwx------ 2 root     root     16384 2008-11-17 10:36 lost+found
drwxr-xr-x 3 ngenplay ngenplay  4096 2011-01-11 14:50 ngenplay
drwxr-xr-x 3 ngensimu ngensimu  4096 2011-01-11 14:48 ngensimu

390°/3903 人阅读/0 条评论 发表评论

登录 后发表评论