sed    stream editor for filtering and transforming text  流编辑器来过滤和改变文本

选项:

-n 安静模式,在标准输入给sed时,加上-n 只输出由sed编辑的那行

-e 直接在命令行模式上进行sed的动作编辑

-f 将sed的动作写入到一个文件中,使用-f filename来执行文件中的sed动作

-i 直接编辑文件文件,而不是输出到屏幕上

-r 支持扩展正则表达式,默认是基础正则表达式

动作:

[n1,[n2]][动作]    n1 n2是行号

动作有 增 删 改 替换

实例1:新增行   'n1a   字符串',新增到本行的下一行

[root@www ~]# sed '2a This is a test' test.txt
The sunset prints sorrow colour with the world
Looking at the reflection in the mirror
This is a test
and feelings become so vacuous
Escape from the darkness
Where are you going toward?
Staring at the fantanstic sky
Stars are shining as a guide
It reminds me of your smile
Suddenly the rain drops from the sky
Just like rose blooming in a mire
Recollecting the most valuable memory
Are those things only a mirage?
We'll be with you and feel your mind
From the moment we wake up to the last
Has he become your lifeline?
Though nobody can ga against time
We still wish
We do wish you and he could stay
forever
forever as one.
[root@www ~]#

实例2:替换行   '2,5c  Num2-5lines replace'

[root@www ~]# sed '2,5c Num2-5lines replace' test.txt
The sunset prints sorrow colour with the world
Num2-5lines replace
Staring at the fantanstic sky
Stars are shining as a guide
It reminds me of your smile
Suddenly the rain drops from the sky
Just like rose blooming in a mire
Recollecting the most valuable memory
Are those things only a mirage?
We'll be with you and feel your mind
From the moment we wake up to the last
Has he become your lifeline?
Though nobody can ga against time
We still wish
We do wish you and he could stay
forever
forever as one.
[root@www ~]#

实例3:删除   '2,5d'

[root@www ~]# sed '2,5d' test.txt
The sunset prints sorrow colour with the world
Staring at the fantanstic sky
Stars are shining as a guide
It reminds me of your smile
Suddenly the rain drops from the sky
Just like rose blooming in a mire
Recollecting the most valuable memory
Are those things only a mirage?
We'll be with you and feel your mind
From the moment we wake up to the last
Has he become your lifeline?
Though nobody can ga against time
We still wish
We do wish you and he could stay
forever
forever as one.
[root@www ~]#

实例4:插入  '2i 字符串'  插入到本行的上一行

[root@www ~]# sed '2i This is test i' test.txt   #若增加-n 选项则只输出插入的此行
The sunset prints sorrow colour with the world
This is test i
Looking at the reflection in the mirror
and feelings become so vacuous
Escape from the darkness
Where are you going toward?
Staring at the fantanstic sky
Stars are shining as a guide
It reminds me of your smile
Suddenly the rain drops from the sky
Just like rose blooming in a mire
Recollecting the most valuable memory
Are those things only a mirage?
We'll be with you and feel your mind
From the moment we wake up to the last
Has he become your lifeline?
Though nobody can ga against time
We still wish
We do wish you and he could stay
forever
forever as one.
[root@www ~]#

实例5:打印 '4,9p' 输出到屏幕第4行到第9行

[root@www ~]# sed -n '4,9p' test.txt   # 若不加-n选项,则会输出全部的行
Escape from the darkness
Where are you going toward?
Staring at the fantanstic sky
Stars are shining as a guide
It reminds me of your smile
Suddenly the rain drops from the sky

实例6:替换(可使用正则的) s     表达式   n1,n2s/old/new/g

[root@www ~]# sed '1,5s/the/a/g' test.txt
The sunset prints sorrow colour with a world
Looking at a reflection in a mirror
and feelings become so vacuous
Escape from a darkness
Where are you going toward?
Staring at the fantanstic sky
Stars are shining as a guide
It reminds me of your smile
Suddenly the rain drops from the sky
Just like rose blooming in a mire
Recollecting the most valuable memory
Are those things only a mirage?
We'll be with you and feel your mind
From the moment we wake up to the last
Has he become your lifeline?
Though nobody can ga against time
We still wish
We do wish you and he could stay
forever
forever as one.
[root@www ~]#

实例7:取出本机ip

[root@www ~]# ifconfig eth0| grep "inet addr"|sed 's/^.*inet addr://g'|sed 's#Bcast.*$##g'
192.168.6.6
[root@www ~]# ifconfig eth0 | sed  -n "2s#^.*addr:##gp"|sed 's#  B.*$##g'
192.168.6.6
[root@www ~]# ifconfig eth0 | sed -nr 's#.*addr:(.*)  B.*$#\1#gp'                #小括号内的匹配。用\1去除。如果有第二个小括号,可以用\2取出
192.168.6.6
[root@www ~]#

实例8:-i  直接编辑文本,而不是输出到屏幕

[root@www ~]# cp test.txt test1.txt
[root@www ~]# sed -i '2,8d' test1.txt
[root@www ~]# cat test1.txt                #没有了2到8行
The sunset prints sorrow colour with the world
Suddenly the rain drops from the sky
Just like rose blooming in a mire
Recollecting the most valuable memory
Are those things only a mirage?
We'll be with you and feel your mind
From the moment we wake up to the last
Has he become your lifeline?
Though nobody can ga against time
We still wish
We do wish you and he could stay
forever
forever as one.
[root@www ~]#

实例9.将/etc/passwd文件中用户名与shell互换位置

[root@www ~]# sed -nr 's#([^:]+)(:.*:)(/.*$)#\3\2\1#gp' /etc/passwd