假设文档内容如下:
[root@localhost ~]# cat /tmp/input.txt
null
000011112222 test

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

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

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

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

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

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

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

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

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

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

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

[root@localhost ~]# sed -i 's/$/&tail/' /tmp/input.txt
[root@localhost ~]# cat /tmp/input.txt
HEADnulltail
HEAD000011112222tail
HEADtail
HEADtesttail

说明:
1."^"代表行首,"$"代表行尾
2.'s/$/&tail/g'中的字符g代表每行出现的字符全部替换,如果想在特定字符处添加,g就有用了,否则只会替换每行第一个,而不继续往后找。

最新文章

  1. 详解在Visual Studio中使用git版本系统[转]
  2. C# RFID windows 服务 串口方式
  3. [codeforces 260]B. Ancient Prophesy
  4. XML文件操作学习(一)
  5. ProgressBar 各种样式
  6. PHP替换数据库的换行符
  7. HDU -1284钱币兑换
  8. 浅谈JSP(二)
  9. SSH框架总结(框架分析+环境搭建+实例源码下载)(转)
  10. OpenStack修复影响宿主机的QEMU漏洞CVE-2017-2615
  11. bsp makefile2
  12. Linux生成私钥和公钥免密连接
  13. Python课程第五天作业
  14. io系列之常用流二
  15. Python数据分析中对重复值、缺失值、空格的处理
  16. springboot org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
  17. 通用Mapper
  18. memcached stats 命令
  19. Redis为什么是单线程
  20. 原型链中的prototype、__proto__和constructor的关系

热门文章

  1. RPM包效验
  2. UEP-标签
  3. C#采用rabbitMQ搭建分布式日志系统
  4. Cookie实现--用户上次访问时间
  5. 微信小程序实现顶部、底部联动滑动
  6. python实现冒泡排序和快速排序
  7. mysql根据汉字首字母排序[转]
  8. 实现iota函数
  9. Can’t open /dev/* exclusively. Mounted filesystem?解决
  10. oracle 查看最大连接数与当前连接数