Linux 中 awk脚本输出文本的最后几列和前几列
001、 输出文本的最后几列
[root@pc1 test]# ls a.txt [root@pc1 test]# 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 test]# awk -F "\t" -v OFS="\t" '{for(i = NF -2; i <= NF; i++){printf "%s%s", $i, (i==NF?ORS:OFS)}}' a.txt ## 利用条件表达式判断$i后输出分隔符还是换行符 04 05 06 10 11 12 16 17 18 22 23 24 28 29 30

。
002、输出文本的前几列
[root@pc1 test]# ls a.txt [root@pc1 test]# 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 test]# awk '{for(i = 1; i <= 4; i++) {printf "%s%s", $i, (i==4?ORS:OFS)}}' a.txt 01 02 03 04 07 08 09 10 13 14 15 16 19 20 21 22 25 26 27 28 [root@pc1 test]# awk -F "\t" -v OFS="\t" '{for(i = 1; i <= 4; i++) {printf "%s%s", $i, (i==4?ORS:OFS)}}' a.txt ## 输出前4列 01 02 03 04 07 08 09 10 13 14 15 16 19 20 21 22 25 26 27 28

。

浙公网安备 33010602011771号