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 @   小鲨鱼2018  阅读(220)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-09-12 linux 中如何列出当前目录中所有文件及目录的绝对路径
2022-09-12 双端测序中reads1和reads2是什么关系
2022-09-12 linux 系统中 wc命令
点击右上角即可分享
微信分享提示