zoukankan      html  css  js  c++  java
  • linux中sed在指定字符前后添加内容

    假设文档内容:

    1 [root@localhost ~]# cat /tmp/input.txt
    2  null
    3 000011112222
    4
    5 test

    1.要求:在1111之前添加AAA,方法如下:

    sed -i 's/指定的字符/要插入的字符&/'  文件

    1 [root@localhost ~]# sed -i  's/1111/AAA&/' /tmp/input.txt                     
    2 [root@localhost ~]# cat /tmp/input.txt                   
    3  null
    4  0000AAA11112222
    5 
    6  test

    2、要求:在1111之后添加BBB,方法如下:

    sed -i 's/指定的字符/&要插入的字符/'  文件

    1 [root@localhost ~]# sed -i  's/1111/&BBB/' /tmp/input.txt    
    2 [root@localhost ~]# cat /tmp/input.txt                   
    3 null
    4 0000AAA1111BBB2222
    5 
    6 test

    3、要求:(1) 删除所有空行;(2) 一行中,如果包含"1111",则在"1111"前面插入"AAA",在"11111"后面插入"BBB"

    1 [root@localhost ~]# sed '/^$/d;s/1111/AAA&/;s/1111/&BBB/' /tmp/input.txt   
    2 null
    3 0000BBB1111AAA2222
    4 test

     4、要求:在每行的头添加字符,比如"HEAD",命令如下:

    1 [root@localhost ~]# sed -i 's/^/HEAD&/' /tmp/input.txt 
    2 [root@localhost ~]# cat /tmp/input.txt
    3 HEADnull
    4 HEAD000011112222
    5 HEAD
    6 HEADtest

      5、要求:在每行的尾部添加字符,比如"tail",命令如下:

    1 [root@localhost ~]# sed -i 's/$/&tail/' /tmp/input.txt      
    2 [root@localhost ~]# cat /tmp/input.txt                
    3 HEADnulltail
    4 HEAD000011112222tail
    5 HEADtail
    6 HEADtesttail
  • 相关阅读:
    JAVA日报
    剑指 Offer 31. 栈的压入、弹出序列
    剑指 Offer 30. 包含min函数的栈
    剑指 Offer 29. 顺时针打印矩阵
    20210426日报
    20210423日报
    20210422日报
    20210421日报
    20210420日报
    20210419日报
  • 原文地址:https://www.cnblogs.com/carey9420/p/14187597.html
Copyright © 2011-2022 走看看