Reputation: 107
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.
Upvotes: 0
Views: 67
Reputation: 3125
As explained in this link, you can use the split()
function to get an array for every index of an awk
multidimensional array because every index of the form
key1,key2,···,keyN
is stored as a string
key1[SUBSEP]key2[SUBSEP]···[SUBSEP]keyN
where [SUBSEP]
stands for the ASCII character hold in the SUBSEP
variable, which by default is \034
. In fact you can access any multidimensional array element alternatively using that string:
x=myarray[key1 SUBSEP key2]
Basically the END
block should look similar to this
END {
for (scan in scansum) {
split(scan,ind,SUBSEP)
print "Scanner: " ind[1] "\tHour: " ind[2] "\tCount Test: " scansum[ind[1],ind[2]]
}
}
Upvotes: 1