這裏舉一些sed經常使用的高級用法例子經供參考:vim
一下操做都針對file.txt文件做修改緩存
[root@QX-××× ~]# cat file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch tzdata-2013g-1.el6.noarch jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-common-0.6.3-32.el6.noarch
匹配行後添加字段:bash
[root@QX-××× ~]# sed 's/noarch$/&.bak/' file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch.bak tzdata-2013g-1.el6.noarch.bak jakarta-commons-collections-3.2.1-3.4.el6.noarch.bak filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch.bak xml-common-0.6.3-32.el6.noarch.bak
標記字符串連接修改值:\1代替 (file) 拼接SYSTEM替換systemide
[root@QX-××× ~]# sed -n 's/\(file\)system/\1SYSTEM/gp' file.txt fileSYSTEM-2.4.30-3.el6.x86_64 mesa-dri-fileSYSTEM-9.2-0.5.el6.x86_64 foomatic-db-fileSYSTEM-4.0-7.20091126.el6.noarch
跨行匹配:this
[root@QX-××× ~]# sed -n '/libgcc/,/system/p' file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch tzdata-2013g-1.el6.noarch jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64
多重-e編輯spa
[root@QX-××× ~]# sed -e '1,3d' -e 's/common/COMMON/' file.txt jakarta-COMMONs-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-COMMON-0.6.3-32.el6.noarch
r讀,w寫,a追加下一行,i追加上一行,c替換3d
[root@QX-××× ~]# cat scc #################### # test file # #################### [root@QX-××× ~]# sed '/tzdata/r scc' file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch tzdata-2013g-1.el6.noarch #################### # test file # #################### jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-common-0.6.3-32.el6.noarch [root@QX-××× ~]# sed '/tzdata/w scc' file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch tzdata-2013g-1.el6.noarch jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-common-0.6.3-32.el6.noarch [root@QX-××× ~]# cat scc tzdata-2013g-1.el6.noarch [root@QX-××× ~]# sed '/tzdata/a\ --> 追加在匹配行的下一行' file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch tzdata-2013g-1.el6.noarch --> 追加在匹配行的下一行 jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-common-0.6.3-32.el6.noarch
n匹配行的下一行,y是轉換 (這裏的n不是參數,是command)orm
[root@QX-××× ~]# sed -n '/tzdata/{n;s/common/COMMON/p;}' file.txt jakarta-COMMONs-collections-3.2.1-3.4.el6.noarch [root@QX-××× ~]# sed '1,3y/abcd/ABCD/' file.txt liBgCC-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noArCh tzDAtA-2013g-1.el6.noArCh jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-common-0.6.3-32.el6.noarch
h 暫存緩存區,g取用xml
[root@QX-××× ~]# sed -e '/libgcc/h' -e '$g' file.txt libgcc-4.4.7-4.el6.x86_64 setup-2.8.14-20.el6_4.1.noarch tzdata-2013g-1.el6.noarch jakarta-commons-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch libgcc-4.4.7-4.el6.x86_64
sed腳本:three
sed腳本就是寫在文件中哈的一列sed命令,在命令啓動sed命令時,若是想讓sed知道這些命令來自文件,就要用-f選項帶上sed腳本的文件名,
sed腳本語法:
末尾不能有任何多餘的空格或文本
若是命令不能獨佔一行,就必須以分號結尾
sed 續行須要用到反斜槓\
vim read1 #my sed script /libgcc/i\ this is add lines one\ two\ three\ add end 1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ s/common/COMMON/g
結果:
[root@QX-××× ~]# sed -f read1 file.txt LIBGCC-4.4.7-4.EL6.X86_64 this is a add lines dsadas sthree SETUP-2.8.14-20.EL6_4.1.NOARCH TZDATA-2013G-1.EL6.NOARCH jakarta-COMMONs-collections-3.2.1-3.4.el6.noarch filesystem-2.4.30-3.el6.x86_64 mesa-dri-filesystem-9.2-0.5.el6.x86_64 foomatic-db-filesystem-4.0-7.20091126.el6.noarch xml-COMMON-0.6.3-32.el6.noarch