zoukankan      html  css  js  c++  java
  • linux系统中sed命令在指定行前(后)插入内容

    1、测试数据如下:

    [root@centos79 test]# ls
    a.txt
    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s

    2、在第2行后插入xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '2a xxxx' a.txt
    3 4 5
    d g 3
    xxxx
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s

    3、在第二行前插入xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '2i xxxx' a.txt
    3 4 5
    xxxx
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s

    4、在第2行到第4行后都插入xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '2,4a xxxx' a.txt
    3 4 5
    d g 3
    xxxx
    s g 8
    xxxx
    k s g
    xxxx
    2 5 d
    s c w
    a r t
    e 4 s

    5、在第二行和第四行后插入xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed -e '2a xxxx' -e '4a xxxx' a.txt
    3 4 5
    d g 3
    xxxx
    s g 8
    k s g
    xxxx
    2 5 d
    s c w
    a r t
    e 4 s

    6、在行首、行尾添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '1i xxxx' a.txt
    xxxx
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '$a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    xxxx

    7、在奇数行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '1~2a xxxx' a.txt
    3 4 5
    xxxx
    d g 3
    s g 8
    xxxx
    k s g
    2 5 d
    xxxx
    s c w
    a r t
    xxxx
    e 4 s

    8、在偶数行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '2~2a xxxx' a.txt
    3 4 5
    d g 3
    xxxx
    s g 8
    k s g
    xxxx
    2 5 d
    s c w
    xxxx
    a r t
    e 4 s
    xxxx

    9、在3倍数行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '3~3a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    xxxx
    k s g
    2 5 d
    s c w
    xxxx
    a r t
    e 4 s

    10、在匹配d的行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '/d/a xxxx' a.txt
    3 4 5
    d g 3
    xxxx
    s g 8
    k s g
    2 5 d
    xxxx
    s c w
    a r t
    e 4 s

    11、在以s开头的行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '/^s/a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    xxxx
    k s g
    2 5 d
    s c w
    xxxx
    a r t
    e 4 s

    12、在以d结尾的行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '/d$/a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    xxxx
    s c w
    a r t
    e 4 s

    13、在以s开头同时以w结尾的行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '/^s.*w$/a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    xxxx
    a r t
    e 4 s

    14、在同时包含k后者w的行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '/k|w/a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    xxxx
    2 5 d
    s c w
    xxxx
    a r t
    e 4 s

    15、在同时包含4和e的行后添加xxxx

    [root@centos79 test]# cat a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    [root@centos79 test]# sed '/4.*e|e.*4/a xxxx' a.txt
    3 4 5
    d g 3
    s g 8
    k s g
    2 5 d
    s c w
    a r t
    e 4 s
    xxxx
  • 相关阅读:
    [转]PostgreSQL数据类型
    Linux下执行自定义的可执行命令无效原因
    [其它]iOS 12.2支持电信VoLTE了,中国电信教你如何开通:只要三步
    本机无法访问虚拟机里面的nginx的80端口
    百度的网络接入架构图
    如何让局域网中的其他主机访问虚拟机
    java中synchronized 用在实例方法和对象方法上面的区别
    Redis登陆服务器和批量删除指定的key
    vim查找关键字的好方法
    网络攻防之动态修改表单的值
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14988775.html
Copyright © 2011-2022 走看看