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
。
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-09-12 linux 中如何列出当前目录中所有文件及目录的绝对路径
2022-09-12 双端测序中reads1和reads2是什么关系
2022-09-12 linux 系统中 wc命令