Linux 中 sed G选项在行后添加空行

 

001、 简单测试

[root@localhost test]# ls
a.txt
[root@localhost test]# cat a.txt           ## 测试数据
dfghghj
hgfdwe
[root@localhost test]# sed 'G' a.txt       ## G在每一行添加空行
dfghghj

hgfdwe

[root@localhost test]# sed 'G;G' a.txt
dfghghj


hgfdwe


[root@localhost test]# sed 'G;G;G' a.txt
dfghghj



hgfdwe

 。

 

002、指定具体行

[root@localhost test]# ls
a.txt
[root@localhost test]# cat a.txt                        ## 测试数据
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
16 17 18
[root@localhost test]# sed '2G' a.txt                   ## 在第二行后添加空行
01 02 03
04 05 06

07 08 09
10 11 12
13 14 15
16 17 18
[root@localhost test]# sed '2,4G' a.txt                 ## 在第二行到第四行后所有行后添加空行
01 02 03
04 05 06

07 08 09

10 11 12

13 14 15
16 17 18
[root@localhost test]# sed '2;4G' a.txt
sed: -e expression #1, char 2: unknown command: `;'
[root@localhost test]# sed '2G;4G' a.txt               ## 在第二行和第四行后添加空行
01 02 03
04 05 06

07 08 09
10 11 12

13 14 15
16 17 18

 。

 

003、

[root@localhost test]# ls
a.txt
[root@localhost test]# cat a.txt                           ## 测试数据
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
16 17 18
[root@localhost test]# sed 's/$/\n/' a.txt                 ## 在每行后添加空行
01 02 03

04 05 06

07 08 09

10 11 12

13 14 15

16 17 18

[root@localhost test]# sed 's/$/\n\n/' a.txt      
01 02 03


04 05 06


07 08 09


10 11 12


13 14 15


16 17 18

 。

 

posted @ 2024-08-22 13:37  小鲨鱼2018  阅读(10)  评论(0编辑  收藏  举报