GUN sed高级用法,sed脚本编写

  • A+
所属分类:安全闲碎

这里举一些sed常用的高级用法例子经供参考:
一下操作都针对file.txt文件作修改
[[email protected] ~]# 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
    匹配行后添加字段:
[[email protected] ~]# 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替换system
[[email protected] ~]# 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
    跨行匹配:
[[email protected] ~]# 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编辑
[[email protected] ~]# 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替换
[[email protected] ~]# cat scc
####################
# test file #
####################
[[email protected] ~]# 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
  
[[email protected] ~]# 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
[[email protected] ~]# cat scc
tzdata-2013g-1.el6.noarch
[[email protected] ~]# 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)
[[email protected] ~]# sed -n '/tzdata/{n;s/common/COMMON/p;}' file.txt
jakarta-COMMONs-collections-3.2.1-3.4.el6.noarch
[[email protected] ~]# 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取用
[[email protected] ~]# 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脚本:
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
    结果:
[[email protected] ~]# 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

本文始发于微信公众号(飓风网络安全):GUN sed高级用法,sed脚本编写

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: