首页 > 其他分享 >sed命令在指定行前(后)插入内容

sed命令在指定行前(后)插入内容

时间:2023-06-06 21:24:34浏览次数:27  
标签:xxxx 行前 插入 sed centos79 test txt root

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,test,txt,root
From: https://www.cnblogs.com/zhugq02/p/17461750.html

相关文章

  • 8.插入排序
      插入排序算法是一种简单的排序算法,也成为直接插入排序算法。它是一种稳定的排序算法,对局部有序的数据具有较高的效率。  插入排序算法是一个对少量元素进行排序的有效算法。比如,打牌是我们使用插入排序方法最多的日常生活例子。我们在摸牌时,一般会重复一下步骤。起初,我们手......
  • linux sed文本内容
    目录一、sed命令二、sed操作符三、sed打印四、sed删除五、sed替换六、sed添加 七、实验演示1.提取版本号2.查看指定时间日志               一、sed命令-e执行多个命令-f使用指定脚本在处理输入文件-h显示帮助......
  • Linux sed 命令的使用方法
    1、linuxsed命令详解2、sed命令http://man.linuxde.net/sed3、Sedandawk笔记之sed篇:基础命令http://kodango.com/sed-and-awk-notes-part-3sed-i"s/zk.addr=.*/zk.addr=$ZOOKEEPER_IP/g"$adapter_conf表示被替换的内容zk.addr=.*表示替换后的内容zk.addr=$ZOOKEEPER_......
  • 字符集问题(mybatis 插入mysql中文乱码,入参是中文)
    1.启动/停止/重启/状态servicemysqldstartservicemysqldstopservicemysqldrestartservicemysqldstatus  mysqld是守护进程脚本,init.d不是mysql的home2.home/进入控制台/usr/lib64/mysqlmysql-uroot-p切换数据库usesomedb查看该数据库字符集......
  • 【HarmonyOS】关于 Caused by java.lang.IllegalStateException The specified...
    【问题描述】线上收到大量手机的崩溃异常,以华为手机为主,崩溃如下1.Causedby:java.lang.IllegalStateException:Thespecifiedmessagequeuesynchronizationbarriertokenhasnotbeenpostedorhasalreadybeenremoved.2.atandroid.os.MessageQueue.removeSyncBarri......
  • mybatis-plus扩展extend批量操作(自带批量操作是循环单条插入,效率太低)
    目录添加依赖构建三个配置-推荐放一个包里面让原本继承BaseMapper<实体>的Dao层改为继承EasyBaseMapper<实体>service层已经可以使用批量操作了 添加依赖<!--mybatis-plus组件--><dependency><groupId>com.baomidou</groupId><artifactId>myba......
  • mac M2 使用sed
    安装distributionBase=GRADLE_USER_HOME替换apline源set-eux&&sed-i's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g'/etc/apk/repositories替换urlsed-es"https://mirrors.cloud.tencent.com/gradle/gradle-6.8.1-bin.zip"/"f......
  • Lattice-Based Group Signatures With Time-Bound Keys via Redactable Signatures
    ......
  • linux sed命令详解
    评:Java代码收藏代码1.Sed简介sed是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并......
  • mybatis-plus 批量插入方法saveBatch 踩坑
    1、问题描述由于我在数据库的一张表设置了两个主键,所以创建的实体我想都加上@TableId注解但是这样在mybatis-plus中一个实体只能有一个@TableId注解标识的主键2、然后我在批量插入时就遇到了问题,我使用的saveBatch方法进行的批量插入,在插入时实体的两个id我都设置值了,但是......