awk: 函数 asorti 从未定义

awk: function asorti never defined

我正在编写 awk 程序,程序需要对 "associative array"

进行排序
{
    subj[]++  
}
END{

    n=asorti(subj, sorted)
    for(k=1;k<=n;k++)
    {
        print(k" "sorted[k])
    }
}

同时执行以下命令

$ awk -f prg.awk "subjects"

给出以下错误:

awk: 函数分类从未定义

[输入文件]

Phy
Math
Math
Science
Bio
Phy

操作系统:ubuntu16.04

排除o/p

Bio 1
Math 2
Phy 2
Science 1

在 Gnu awk 中,您可以使用 PROCINFO["sorted_in"]:

设置 for 扫描顺序
$ cat > foo.awk
{
    a[]++
} 
END {
    PROCINFO["sorted_in"]="@ind_str_asc"  # scanning in index ascending order
    for(i in a)                           # for here uses above defined order
        print i,a[i]
}
Bio 1
Math 2
Phy 2
Science 1

https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html