美文网首页
sed日常处理脚本

sed日常处理脚本

作者: hbtszyt | 来源:发表于2018-09-06 10:16 被阅读0次

    1.在某行的前一行或者后一行添加内容

    比如在t文件中,allow 361way.com行之前添加allow www.361way.com

    #匹配行前加

    sed -i '/allow 361way.com/iallow www.361way.com' the.conf.file

    #匹配行前后

    sed -i '/allow 361way.com/aallow www.361way.com' the.conf.file

    在写脚本时候,一般为了方便,会在i和a后边加一个反斜杠,为了方便区分

    sed -i '/2222222222/a\3333333333' test.txt

    sed -i '/2222222222/i\3333333333' test.txt

    这就就可以很方便的看出要在某一行前或某一行后加入什么内容 。不过经常我记不住a 、i 那个是前那个是后。我的记法是a = after ,i = in front

    而且其可以配合find查找的内容处理,如下:

    find . -name server.xml|xargs sed -i '/directory/i      '

    find . -name server.xml|xargs sed -i '/pattern="%h/a -->'

    2.在某行(指具体行号)前或后加一行内容

    sed -i 'N;4addpdf' a.txt

    sed -i 'N;4ieepdf' a.txt

    (4代表第几行,a和i代表之前还是之后,ddpdf和eepdf代表要插入的内容)  a.txt指的是文件

    3.删除,显示,模式查询

    删除第一行:sed '1d' test.txt,删除最后一行:sed '$d' test.txt,删除第一行到第二行:sed '1,2d' test.txt,删除第二行到最后一行: sed '2,$d' test.txt

    显示第一行 :sed -n '1p' test.txt,显示最后一行:sed -n '$p' test.txt ,显示第一行到第二行:sed -n '1,2p' test.txt ,显示第二行到最后一行:sed -n '2,$p' test.txt

    查询包括关键字ruby所在所有行:sed -n '/ruby/p' test.txt,查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义:sed -n '/\$/p' test.txt 

    相关文章

      网友评论

          本文标题:sed日常处理脚本

          本文链接:https://www.haomeiwen.com/subject/hwutgftx.html