For loop 循环中的awk和

For loop 循环中的awk和,for-loop,awk,For Loop,Awk,我有这个剧本: #!/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[

我有这个剧本:

 #!/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"
}
我还想打印第二个字段的总和,但请注意计数数量从1到多不等。for循环中应该有一个while循环来总结计数,还是在脚本末尾进行计算

感谢您提供的任何提示

下面是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         *****

下面的
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         *****

下面的
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         *****

基本上应该是:

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

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

基本上应该是:

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


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

请在您的帖子中的代码标记中发布示例输入文件。@RavinderSingh13,我添加了一些数据。我希望它对你有用。你要求第二个字段的总和,但你已经显示了第四个字段的数据,请发布完整的输入文件,而不是发布部分。输入文件已发布。我要求使用for循环创建的count字段来计算结果并在最后打印。我很好奇-你怎么看
i3请在你的帖子中的代码标记中发布示例输入文件。@RavinderSingh13,我添加了一些数据。我希望它对你有用。你要求第二个字段的总和,但你已经显示了第四个字段的数据,请发布完整的输入文件,而不是发布部分。输入文件已发布。我要求使用for循环创建的count字段来计算结果并在最后打印。我很好奇-你认为
i3这是正确打印count和amount列,但总和不正确。请注意,有两条80金额记录,但只有一条被计数。另外有趣的是,您正在打印结尾部分的标题列信息。@RegisteredUser,不,我得到的80的计数是2。请检查我在运行代码后得到的编辑输出。您是对的,计数是正确的,但总和是错误的。@RegisteredUser,请立即检查。实际上,由于它在数组中只取一个唯一的计数,所以它跳过了所有计数的和。现在已添加。这将正确打印计数和金额列,但总和不正确。请注意,有两条80金额记录,但只有一条被计数。另外有趣的是,您正在打印结尾部分的标题列信息。@RegisteredUser,不,我得到的80的计数是2。请检查我在运行代码后得到的编辑输出。您是对的,计数是正确的,但总和是错误的。@RegisteredUser,请立即检查。实际上,由于它在数组中只取一个唯一的计数,所以它跳过了所有计数的和。现在添加。谢谢您的回答!我不知道
length()
函数-非常方便。出于可读性考虑,我让你的答案成为可以接受的答案。
n=asorti(a,b,“@val_num_asc”);对于(i=1;i@RegisteredUser不客气。提供一个简单易读的答案是我的初衷。:)谢谢你的回答!我不知道
length()
函数-非常方便。出于可读性考虑,我让你的答案成为可以接受的答案。
n=asorti(a,b,“@val_num_asc”);对于(i=1;i@RegisteredUser不客气。提供一个简单易懂的答案是我的初衷。:)