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
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14988775.html