首页 > 系统相关 >linux系统中sed命令在指定行前(后)插入内容

linux系统中sed命令在指定行前(后)插入内容

时间:2022-11-22 15:22:31浏览次数:42  
标签:xxxx 行前 sed centos79 linux test txt root

https://www.cnblogs.com/liujiaxin2018/p/14988775.html

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

 

标签:xxxx,行前,sed,centos79,linux,test,txt,root
From: https://www.cnblogs.com/xincha/p/16915230.html

相关文章

  • Linux Python Web 离线部署非 Docker部署
    实际项目中,避免不了遇到私有化部署。不能在线安装有些问题,项目中各种包需要很多依赖非常痛苦。如果,项目支持容器化部署这个是最简单的。以下介绍离线安装Python包、包相关d......
  • LINUX和UNIX主要发行版本
    UNIX版本操作系统公司硬件平台AIXIBMPowerPCHP-UXHPPA-RISCSolarisSunSPARCLINUXRedHatLINUX等等IA等 LINUX内核版本Linux内核官网:www.kernel.org......
  • Linux 主机意外重启原因
    查看主机是正常重启还是意外重启:last-x|head|tac正常reboot重启显示如下:rootpts/010.5.3.207TueNov2211:14-down(00:04)shutdownsy......
  • Linux笔记
    Linux操作系统的开机过程:从BIOS开始,然后进入BootLoader,再加载系统内核,然后内核进行初始化,最后启动初始化进程RHEL7采用​​​systemd​​初始化进程服务。checkdate......
  • Linux7系统安装Docker服务
    一.环境准备1.系统介绍系统:centos7.9内核版本:3.10.0-11602.yum更新(不是必须的,后面出现不兼容的情况再update)yum-yupdate#升级所有包同时也升级软件和系统内核;yum-......
  • linux入门
    [Linux就该这么学第二版.pdf](https://www.yuque.com/attachments/yuque/0/2022/pdf/29649025/1658236286875-967f377b-1385-4c5c-af57-fc905c842d5d.pdf)##打包和压......
  • 用C++写一遍linux socket通信过程
    要想深刻理解一些技术的底层,我还是觉得非用用c/c++写一遍的比较好。这其中的原因相信懂的人都懂。回忆一下上学时候在c语言课堂上的激动劲,如今感觉对c的理解真的都是那时候......
  • 一步一图带你深入理解 Linux 物理内存管理
    1.前文回顾在上篇文章《深入理解Linux虚拟内存管理》中,笔者分别从进程用户态和内核态的角度详细深入地为大家介绍了Linux内核如何对进程虚拟内存空间进行布局以及......
  • linux服务器上,如何查看日志和查找问题?
    前言有些高频日志无法输出到数据库,走中间件异步到数据库可能会有延迟。所以,有时候需要将日志直接输出到控制台,混合了大多系统info后,要如何定位呢。姿势cpxxx.logtmpcattm......
  • 下载go源码时,遇到git - error: RPC failed; curl 18 transfer closed with outstandin
    执行下条语句时,出现该错误gitclonehttps://go.googlesource.com/go解决方案:gitconfig--globalhttp.postBuffer524288000......