【发布时间】: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<= 5在for (i=4; i 3<= 5; i++ )中是什么意思?