Linux 中sed命令实现指定列的加倍

 

001、第一列

[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      19      20
[root@localhost test]# sed 's/^\S\+/&\t&/' a.txt        ## 第一列数据加倍
01      01      02      03      04
05      05      06      07      08
09      09      10      11      12
13      13      14      15      16
17      17      18      19      20
[root@localhost test]# sed 's/^\S\+/&\t&\t&/' a.txt      ## 第一列数据加倍
01      01      01      02      03      04
05      05      05      06      07      08
09      09      09      10      11      12
13      13      13      14      15      16
17      17      17      18      19      20

 

 

002、2-x列

[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      19      20
[root@localhost test]# sed 's/[\t ]\+\S\+/&&/1' a.txt        ## 实现第二列数据的加倍
01      02      02      03      04
05      06      06      07      08
09      10      10      11      12
13      14      14      15      16
17      18      18      19      20
[root@localhost test]# sed 's/[\t ]\+\S\+/&&/2' a.txt         ## 实现第三列数据的加倍
01      02      03      03      04
05      06      07      07      08
09      10      11      11      12
13      14      15      15      16
17      18      19      19      20
[root@localhost test]# sed 's/[\t ]\+\S\+/&&&/2' a.txt        ## 实现第三列数据的加倍
01      02      03      03      03      04
05      06      07      07      07      08
09      10      11      11      11      12
13      14      15      15      15      16
17      18      19      19      19      20

 

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      19      20
[root@localhost test]# sed 's/[\t ]\+\S\+$/&&/' a.txt      ## 最后一列
01      02      03      04      04
05      06      07      08      08
09      10      11      12      12
13      14      15      16      16
17      18      19      20      20
[root@localhost test]# sed 's/[\t ]\+\S\+$/&&&/' a.txt       ## 最后一列加倍
01      02      03      04      04      04
05      06      07      08      08      08
09      10      11      12      12      12
13      14      15      16      16      16
17      18      19      20      20      20

 。

 

posted @ 2025-02-16 20:10  小鲨鱼2018  阅读(11)  评论(0)    收藏  举报