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
    

      

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

  • 相关阅读:
    策略模式(Strategy Pattern)
    责任链模式(Chain of Responsibility Pattern)
    单例模式(Singleton Pattern)
    观察者模式(Observer Pattern)
    iOS常用工具类
    iOS常用的封装方法
    iOS中书写代码规范
    UITableViewCell中的使用cell和cell.contentView的区别
    colorWithAlphaComponent
    iOS 压缩图片
  • 原文地址:https://www.cnblogs.com/crazymagic/p/11148533.html
Copyright © 2011-2022 走看看