微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。