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.txtThe sunset prints sorrow colour with the worldLooking at the reflection in the mirrorThis is a testand feelings become so vacuousEscape from the darknessWhere are you going toward?Staring at the fantanstic skyStars are shining as a guideIt reminds me of your smileSuddenly the rain drops from the skyJust like rose blooming in a mireRecollecting the most valuable memoryAre those things only a mirage?We'll be with you and feel your mindFrom the moment we wake up to the lastHas he become your lifeline?Though nobody can ga against timeWe still wishWe do wish you and he could stayforeverforever as one.[root@www ~]#

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

[root@www ~]# sed '2,5c Num2-5lines replace' test.txtThe sunset prints sorrow colour with the worldNum2-5lines replaceStaring at the fantanstic skyStars are shining as a guideIt reminds me of your smileSuddenly the rain drops from the skyJust like rose blooming in a mireRecollecting the most valuable memoryAre those things only a mirage?We'll be with you and feel your mindFrom the moment we wake up to the lastHas he become your lifeline?Though nobody can ga against timeWe still wishWe do wish you and he could stayforeverforever as one.[root@www ~]#

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

[root@www ~]# sed '2,5d' test.txtThe sunset prints sorrow colour with the worldStaring at the fantanstic skyStars are shining as a guideIt reminds me of your smileSuddenly the rain drops from the skyJust like rose blooming in a mireRecollecting the most valuable memoryAre those things only a mirage?We'll be with you and feel your mindFrom the moment we wake up to the lastHas he become your lifeline?Though nobody can ga against timeWe still wishWe do wish you and he could stayforeverforever as one.[root@www ~]#

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

[root@www ~]# sed '2i This is test i' test.txt   #若增加-n 选项则只输出插入的此行The sunset prints sorrow colour with the worldThis is test iLooking at the reflection in the mirrorand feelings become so vacuousEscape from the darknessWhere are you going toward?Staring at the fantanstic skyStars are shining as a guideIt reminds me of your smileSuddenly the rain drops from the skyJust like rose blooming in a mireRecollecting the most valuable memoryAre those things only a mirage?We'll be with you and feel your mindFrom the moment we wake up to the lastHas he become your lifeline?Though nobody can ga against timeWe still wishWe do wish you and he could stayforeverforever as one.[root@www ~]#

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

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

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

[root@www ~]# sed '1,5s/the/a/g' test.txtThe sunset prints sorrow colour with a worldLooking at a reflection in a mirrorand feelings become so vacuousEscape from a darknessWhere are you going toward?Staring at the fantanstic skyStars are shining as a guideIt reminds me of your smileSuddenly the rain drops from the skyJust like rose blooming in a mireRecollecting the most valuable memoryAre those things only a mirage?We'll be with you and feel your mindFrom the moment we wake up to the lastHas he become your lifeline?Though nobody can ga against timeWe still wishWe do wish you and he could stayforeverforever 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 worldSuddenly the rain drops from the skyJust like rose blooming in a mireRecollecting the most valuable memoryAre those things only a mirage?We'll be with you and feel your mindFrom the moment we wake up to the lastHas he become your lifeline?Though nobody can ga against timeWe still wishWe do wish you and he could stayforeverforever as one.[root@www ~]#

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

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