001、for循环
[root@PC1 test02]# ls a.txt [root@PC1 test02]# cat a.txt ## 测试数据 1 2 3 4 5 6 7 8 9 10 11 12 [root@PC1 test02]# awk '{sum = 0; for(i = 1; i < 4; i++) {sum += $i}; print sum}' a.txt ## for循环结构输出每行的和 6 15 24 33
002、while循环结构
[root@PC1 test02]# ls a.txt [root@PC1 test02]# cat a.txt ## 测试数据 1 2 3 4 5 6 7 8 9 10 11 12 [root@PC1 test02]# awk '{sum = 0; i = 1; while(i < 4) {sum += $i; i++}; print sum}' a.txt ## while循环结构 6 15 24 33
003、do 、while结构
[root@PC1 test02]# ls a.txt [root@PC1 test02]# cat a.txt ## 测试数据 1 2 3 4 5 6 7 8 9 10 11 12 [root@PC1 test02]# awk '{sum = 0; i = 1; do {sum += $i; i++} while(i < 4); print sum}' a.txt ## do while结构 6 15 24 33
参考:https://mp.weixin.qq.com/s?__biz=Mzk0MjI2MzA5MQ==&mid=2247488104&idx=1&sn=9aee91c6445861a2a44154e42098f042&chksm=c2c48c34f5b30522a2d46307cbcea31447867af084a82a5f8f9ac63f9f4dc086b7372b103ed0&mpshare=1&scene=23&srcid=0817zYt1GOo6DqHoEaxBXa8X&sharer_sharetime=1692203982361&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd