【问题标题】:awk sum in for loopfor 循环中的 awk 总和
【发布时间】:2018-01-08 06:35:42
【问题描述】:

我有这个脚本:

 #!/usr/local/bin/gawk -f

BEGIN{
    FS="=|,"
    PROCINFO["sorted_in"]="@ind_num_asc";
    printf "\n"
    printf "%-7s %s", "Count", "Amount"
    printf "\n"
    OFS="\t"
}
/phrase/{
    for (i=4; i 3<= 5; i++ )
        if ($i != "") a[$i]++
}

END{
    for (i in a) {print a[i], i; tot++}
    printf "\n"
    printf " ***** %s total wins *****", tot
    printf "\n"
}

产生这个输出:

Count   Amount
1       20
1       22
1       29
1       37
1       38
1       45
1       46
2       80
1       99
1       800

 ***** 10 total unique amounts *****

我还想打印第二个字段的总和,但请注意计数从 1 到多个不等。 for 循环中是否应该有一个 while 循环来总结计数或在脚本末尾的 END 中进行数学运算?

感谢您提供的任何提示!

这是 gawk 提取的示例数据

gawk -F"=|," '/phrase/ {print $4}' file
80
800
20
46
38
45
99
80
29
22
37

解析前的数据示例:

.\phrase(100): [LOG] API context: context=3, amount=80
.\phrase(100): [LOG] API context: context=3, amount=800
.\phrase(100): [LOG] API context: context=3, amount=20
.\phrase(100): [LOG] API context: context=3, amount=46
.\phrase(100): [LOG] API context: context=3, amount=38
.\phrase(100): [LOG] API context: context=3, amount=45
.\phrase(100): [LOG] API context: context=3, amount=99
.\phrase(100): [LOG] API context: context=3, amount=80
.\phrase(100): [LOG] API context: context=3, amount=29
.\phrase(100): [LOG] API context: context=3, amount=22
.\phrase(100): [LOG] API context: context=3, amount=37

预期结果:

Count   Amount
1       20
1       22
1       29
1       37
1       38
1       45
1       46
2       80
1       99
1       800

***** 10 total unique amounts *****
***** 1296 sum totals         *****

【问题讨论】:

  • 请在您的帖子中的代码标签中发布示例 Input_file。
  • @RavinderSingh13,我添加了一些数据。希望对你有用。
  • 您正在询问第二个字段的总和,但您已显示第四个字段的数据,请发布完整的 Input_file 而不是发布其中的片段。
  • 输入文件已发布。我要求使用 for 循环创建的计数字段来计算结果并在最后打印。
  • 我很好奇 - 你认为i 3&lt;= 5for (i=4; i 3&lt;= 5; i++ ) 中是什么意思?

标签: for-loop awk


【解决方案1】:

基本上应该是:

awk -F= '{a[$NF]++;t+=$NF} # You can calculate the total here
         END{
             for(i in a) print a[i], i
             printf "%s uniq\n", length(a)
             printf "%s total\n", t
         }' a.txt

为简洁起见,我省略了对标题的排序和打印。

【讨论】:

  • 感谢您的回答!我不知道length() 函数——非常方便。出于可读性考虑,我正在使您的答案成为可接受的答案。
  • n=asorti(a,b,"@val_num_asc"); for(i=1;i&lt;=n;i++) print a[b[i]],b[i] 将是排序后的版本。
  • @RegisteredUser 欢迎您。提供一个简单易读的答案是我的意图。 :)
【解决方案2】:

关注awk 可能对您有所帮助。

awk -F'=' '{a[$NF]++;sum+=$NF} END{print "Count   Amount";for(i in a){print a[i]"\t"i;};print "***** " length(a),"total unique amounts *****" RS "***** " sum "sum totals         *****"}'  Input_file

输出如下。

Count   Amount
2       80
1       45
1       37
1       46
1       29
1       38
1       20
1       22
1       800
1       99
***** 10 total unique amounts *****
***** 1296sum totals         *****

【讨论】:

  • 这是正确打印计数和金额列,但总和不正确。请注意,有两条 80 金额记录,但只计算一条。
  • 在END部分打印标题栏信息也很有趣。
  • @RegisteredUser,不,我得到 80 的计数是 2。请检查我在运行代码后得到的编辑输出。
  • 你是对的,计数是正确的,但总和是错误的。
  • @RegisteredUser,请立即查看。实际上,因为它在数组中只占用 1 个唯一计数,所以它跳过了所有计数的总和。现在添加它。
猜你喜欢
  • 2014-01-09
  • 2015-06-16
  • 2013-12-28
  • 2023-01-25
  • 2013-06-21
  • 1970-01-01
  • 2023-03-03
  • 2022-01-17
  • 2020-11-07
相关资源
最近更新 更多