linux 中删除文件的倒数第二列

 

001、方法1

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt           ## 测试数据
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# awk '{$(NF - 1) = ""; print $0}' a.txt
01 02 03 04 05 06 07 08  10
11 12 13 14 15 16 17 18  20
21 22 23 24 25 26 27 28  30
[root@pc1 test03]# awk '{$(NF - 1) = ""; print $0}' a.txt | sed 's/[\t ]\+/\t/g'
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

002、方法2

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# awk '{for(i = 1; i <= NF; i++) {if( i != (NF - 1)) {printf(" %s", $i)}} printf("\n")}' a.txt
 01 02 03 04 05 06 07 08 10
 11 12 13 14 15 16 17 18 20
 21 22 23 24 25 26 27 28 30
[root@pc1 test03]# awk '{for(i = 1; i <= NF; i++) {if( i != (NF - 1)) {printf(" %s", $i)}} printf("\n")}' a.txt  | sed 's/^ //'
01 02 03 04 05 06 07 08 10
11 12 13 14 15 16 17 18 20
21 22 23 24 25 26 27 28 30

 

003、方法3

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt         ## 测试数据
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# num=$(head -n 1 a.txt | awk '{print NF - 1}')     ## 求出行号
[root@pc1 test03]# echo $num
9
[root@pc1 test03]# cut -f $num --complement a.txt     ## 利用cut 互补实现
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

004、rev  + cut实现

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# rev a.txt | cut -f 2 --complement | rev
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

005、sed实现

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# sed 's/\(.*\)\(\s\+\S\+\s\+\)\(\S\+$\)/\1\t\3/' a.txt
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# sed -r 's/(.*)(\s+\S+\s+)(\S+$)/\1\t\3/' a.txt        ## sed实现
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

006、awk实现

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# awk '{$(NF - 1) = $NF; sub(/\s*\S*$/, "")}1' a.txt   ## awk替换实现
01 02 03 04 05 06 07 08 10
11 12 13 14 15 16 17 18 20
21 22 23 24 25 26 27 28 30

 

 

posted @ 2023-09-12 17:55  小鲨鱼2018  阅读(166)  评论(0编辑  收藏  举报