• sed追加文件内容的几种方式


    追加用法总结

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

    1.在匹配行后面追加 a

    test文件内容:

    [root@prometheus01 ~]# cat test
    AAAAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    ffffffff
    A1B1c1:
    aaaaaa
    bbbbbb
    /bin/bash
    

    test文件第10行后面追加“add line behind”

    [root@prometheus01 ~]# sed '10aadd line behind' test
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    ffffffff
    A1B1c1:
    add line behind
    aaaaaa
    bbbbbb
    /bin/bash
    

    test文件第10行到第14行,每一行后面都追加“test line behind”

    [root@prometheus01 ~]# sed '10,14atest line behind' test
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    ffffffff
    A1B1c1:
    test line behind
    aaaaaa
    test line behind
    bbbbbb
    test line behind
    /bin/bash
    test line behind
    

    test文件匹配到/bin/bash的行后面追加"insert line for /bin/bash behind"

    [root@prometheus01 ~]# sed '/\/bin\/bash/a insert line for /bin/bash behind' test
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    ffffffff
    A1B1c1:
    aaaaaa
    bbbbbb
    /bin/bash
    insert line for /bin/bash behind
    

    2.在匹配行前面追加 i

    test文件匹配到以hello开头的行,在匹配行前面追加“add line before”

    sed '/^hello/i add line before' test
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    add line before
    hello world!
    ee000
    ffffffff
    A1B1c1:
    aaaaaa
    bbbbbb
    /bin/bash
    

    test文件每一行前面都追加“insert line before every line”

    [root@prometheus01 ~]# sed 'i insert line before every line' test 
    insert line before every line
    AAA
    insert line before every line
    new test!
    insert line before every line
    ccc
    insert line before every line
    345
    insert line before every line
    dddddddd
    insert line before every line
    ccc
    insert line before every line
    hello world!
    insert line before every line
    ee000
    insert line before every line
    ffffffff
    insert line before every line
    A1B1c1:
    insert line before every line
    aaaaaa
    insert line before every line
    bbbbbb
    insert line before every line
    /bin/bash
    

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

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

    [root@prometheus01 ~]# sed '10r /etc/fstab' test
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    ffffffff
    A1B1c1:
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May 10 18:09:26 2021
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=8ec8de38-0b33-4c49-b1df-b93e94499607 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    aaaaaa
    bbbbbb
    /bin/bash
    

    将/etc/fstab文件追加到test文件匹配到/bin/bash行后面

    [root@prometheus01 ~]# sed '/\/bin\/bash/r /etc/fstab' test
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    ffffffff
    A1B1c1:
    aaaaaa
    bbbbbb
    /bin/bash
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May 10 18:09:26 2021
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=8ec8de38-0b33-4c49-b1df-b93e94499607 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    

    将/etc/fstab文件内容追加到test文件中特定行后面,匹配以ee开头的行,到第10行的所有行

    [root@prometheus01 ~]# sed '/^ee/,10r /etc/fstab' test   
    AAA
    new test!
    ccc
    345
    dddddddd
    ccc
    hello world!
    ee000
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May 10 18:09:26 2021
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=8ec8de38-0b33-4c49-b1df-b93e94499607 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    ffffffff
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May 10 18:09:26 2021
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=8ec8de38-0b33-4c49-b1df-b93e94499607 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    A1B1c1:
    
    #
    # /etc/fstab
    # Created by anaconda on Mon May 10 18:09:26 2021
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=8ec8de38-0b33-4c49-b1df-b93e94499607 /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    aaaaaa
    bbbbbb
    /bin/bash
    

    4.将匹配行写入指定文件 w

    将test文件中匹配到/bin/bash的行追加到sed文件中

    [root@prometheus01 ~]# sed -i '/\/bin\/bash/w sed' test   
    [root@prometheus01 ~]# cat sed 
    /bin/bash
    

    将test文件从第10行开始,到匹配到/bin/bash的所有行内容追加到sed-1中

    [root@prometheus01 ~]# sed -i '10,/\/bin\/bash/w sed-1' test  
    [root@prometheus01 ~]# cat sed-1 
    A1B1c1:
    aaaaaa
    bbbbbb
    /bin/bash
    
  • 相关阅读:
    详解prototype、proto和constructor的关系
    BFC
    transition(动画属性)
    React组件生命周期
    正则限制input只能输入大于0的数字
    msbuild编译
    python 在头文件添加 #include "stdafx.h"
    2017年感想
    chVsprintf
    富文本测试
  • 原文地址:https://www.cnblogs.com/even160941/p/16003379.html
Copyright © 2020-2023  润新知