Linux系统vi替换命令的使用

Linux系统vi替换命令的使用

s命令:替换字符串
-------------------------------------------------------
:s/happy/glad/ 或者:s/happy/glad
替换当前行第一个happy为glad

I am VBird
I am very happy to write this class for you!
If you have any question, please contact with me.
VBird 2009/9/12

I am VBird
I am very glad to write this class for you!
If you have any question, please contact with me.
VBird 2009/9/12
-------------------------------------------------------
:s/hi/hello/g
替换当前行所有hi为hello

hi hi hi hi how are you!
hi hi hi hi how are you!

hello hello hello hello how are you!
hi hi hi hi how are you!
-------------------------------------------------------
:n,$s/hi/hello
替换第n行开始到最后一行中每一行的第一个hi为hello

:3,$s/hi/hello

hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!

hi hi hi hi how are you!
hi hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
-----------------------------------------
:n,$s/hi/hello/g
替换第n行开始到最后一行中每一行所有hi为hello
:3,$s/hi/hello/g

hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!

hi hi hi hi how are you!
hi hi hi hi how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
----------------------------------------------
n为数字,若n为(.),表示从当前行开始到最后一行
:.,$s/hi/hello/g

hi hi hi hi how are you!
hi hi hi hi how are you!(当前行)
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!

hi hi hi hi how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
--------------------------------------------------
:%s/hi/hello等同于:g/hi/s//hello

hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!

hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
hello hi hi hi how are you!
----------------------------------------------
:%s/hi/hello/g等同于:%s/hi/s//hello/g

hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!
hi hi hi hi how are you!

hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
hello hello hello hello how are you!
----------------------------------------
可以使用#作为分隔符,此时中间出现的/不会作为分隔符
:s#hi/#hello/
替换当前行第一个hi/为hello/

hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!(当前行)
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!

hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hello/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
hi/ hi/ hi/ hi/ how are you!
--------------------------------------------------------
:%s+/oradata/apras/+/user01/apras1/

/oradata/apras/
/oradata/apras/
/oradata/apras/

/user01/apras1/
/user01/apras1/
/user01/apras1/
--------------------------------------------------------

你可能感兴趣的:(linux)