I have the following array:
scansum[key,key2]++
key
= multiple 6 digit numbers eg. 123456, 234567, 345678 etc.
key2
= a hour eg. 00, 01, 02 etc.
#!/bin/sh
awk '
BEGIN {
printf ("---------------------------------------\n-----CTS_DR Read Report By Scanner-----\n---------------------------------------\n")
}
{
key = substr ($16,3,6) #Scanner
key2 = substr ($2,1,3) #Hour
scanner[key]++
if ($21 ~ /SR1/) scansum[key,key2]++
}
END {
for (scan in scanner) {
printf ("\nScanner: MS%6s\tTotal Reads:%8s\n",scan,scanner[scan])
for (i = 0; i <= 23; i++) {
if (i < 10)
h = "0" i
else
h = i
print "Hour: "h " Count Test: "scansum[scan,h]
}
}
}'
I know that for a normal array you can just do:
for (alias in arrayname) {
print arrayname[alias]
}
and this will do a count of the array split by the key but for a multidimensional array the following doesn't seem to work:
for (alias in arrayname) {
print alias
for (i = 0; i <= 23; i++) {
if (i <10)
h = "0" i
else
h = i
print h"\t"arraynamecount[alias,h]
}
}
Note
I know that this is missing the setting of the array
I know I am missing something but I can't for the life of me see what it is. Any hints/ help for this would be greatly appreciated.
答案 0 :(得分:1)
如this link中所述,您可以使用split()
函数为awk
多维数组的每个索引获取一个数组,因为表单的每个索引都是
key1,key2,···,keyN
存储为字符串
key1[SUBSEP]key2[SUBSEP]···[SUBSEP]keyN
其中[SUBSEP]
代表 SUBSEP
变量中的ASCII字符保留,默认情况下为\034
。实际上,您可以使用该字符串访问任何多维数组元素:
x=myarray[key1 SUBSEP key2]
基本上END
块看起来应与此类似
END {
for (scan in scansum) {
split(scan,ind,SUBSEP)
print "Scanner: " ind[1] "\tHour: " ind[2] "\tCount Test: " scansum[ind[1],ind[2]]
}
}