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
    

      

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

  • 相关阅读:
    Mobile GMaps - Google Map移动版
    365Key今天不能用了,感觉不爽
    推荐:对个人免费的杀毒软件[avast!]
    向 Visual Studio 2005 Tools for Office 迁移时,将 VBA 代码转换为 Visual Basic .NET
    令人向往的3000年生活(转载)
    也说技术人员创业
    痛恨3721的朋友们,装个avast! Antivirus吧
    很Cool很全的Google Map的应用
    关于.net的企业应用开发
    天天网摘(20050704)
  • 原文地址:https://www.cnblogs.com/crazymagic/p/11148533.html
Copyright © 2011-2022 走看看