首页 > 系统服务 > 详细

linux中sed在指定字符前后添加内容

时间:2020-12-25 11:37:36      阅读:336      评论:0      收藏:0      [点我收藏+]

假设文档内容:

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

 

linux中sed在指定字符前后添加内容

原文:https://www.cnblogs.com/carey9420/p/14187597.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!