我正在研究awk程序和程序需要对"关联数组进行排序"
{
subj[$1]++
}
END{
n=asorti(subj, sorted)
for(k=1;k<=n;k++)
{
print(k" "sorted[k])
}
}
虽然低于cmd
$ awk -f prg.awk "subjects"
给出以下错误:
awk:函数asorti从未定义
[inputfile中]
Phy
Math
Math
Science
Bio
Phy
操作系统:ubuntu 16.04
例外o / p
Bio 1
Math 2
Phy 2
Science 1
答案 0 :(得分:1)
在Gnu awk中,您可以使用for
设置PROCINFO["sorted_in"]
扫描顺序:
$ cat > foo.awk
{
a[$1]++
}
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