zoukankan      html  css  js  c++  java
  • sed 追加文件内容

    追加用法总结

    • 1、a 在匹配行后面追加
    • 2、i 在匹配行前面追加
    • 3、r 将文件内容追加到匹配行后面
    • 4、w 将匹配行写入指定文件

    在匹配行后面追加 a 

    passwd文件第10行后面追加"Add Line Behind"

    sed -i '10aAdd Line Behind' passwd 
    

      

    passwd文件第10行到第20行,每一行后面都追加"Test Line Behind"

    sed -i '10,20a Test Line Behind' passwd
    

      

    passwd文件匹配到/bin/bash的行后面追加"Insert Line For /bin/bash Behind"

    sed -i '//bin/bash/a Insert Line For /bin/bash Behind' passwd
    

      

    在匹配行前面追加 i

    passwd文件匹配到以nginx开头的行,在匹配行前面追加"Add Line Before"

    sed -i '/^nginx/i Add Line Before' passwd
    

      

    passwd文件每一行前面都追加"Insert Line Before Every Line"

    sed -i 'a Insert Line Before Every Line' passwd
    

      

    将文件内容追加到匹配行后面 r 

    将/etc/fstab文件的内容追加到passwd文件第20行后面

    sed -i '20r /etc/fstab' passwd
    

      

    将/etc/inittab文件内容追加到passwd文件匹配到/bin/bash行的后面

    sed -i '//bin/bash/r /etc/inittab' passwd
    

    将/etc/vconsole.conf文件内容追加到passwd文件中特定行后面,匹配以ftp开头的行,到第18行的所有行

    sed -i '/^ftp/,18r /etc/vconsole.conf' passwd
    

      

    将匹配行写入指定文件 w

    将passwd文件匹配到/bin/bash的行追加到/tmp/sed.txt文件中

    sed -i '//bin/bash/w /tmp/sed.txt' passwd
    

      

    将passwd文件从第10行开始,到匹配到/sbin/nologin的所有行内容追加到/tmp/sed-1.txt

    sed -i '10,//sbin/nologin/w /tmp/sed-1.txt' passwd
    

      

    混合区间匹配读取内容追加容易出错 在处理几十万上百万的文件中,可以找出特定的行,输出到一个文件中,然后再对这个文件进行处理

  • 相关阅读:
    切换某个窗口为当前窗口并显示在最前面---非置顶
    C语言算法-求两直线夹角计算公式
    Qt编译时MinGW去掉对gcc动态库的依赖
    解决不能从 WTL::CString 转换为 ATL::CSimpleString & 的问题
    gcc编译器对宽字符的识别
    4月10日学习笔记——jQuery选择器
    HTML5
    4月8日学习笔记(js基础)
    网站易用性2
    网站易用性
  • 原文地址:https://www.cnblogs.com/crazymagic/p/11148533.html
Copyright © 2011-2022 走看看