BEGIN { split("",Num,""); #array where we store the numbers Inf= 2**32; #the largest number we can process OFMT="10%s"; Max = -1*Inf; #max count seen in any bucket #initialized to the smallest number Here; #the key of the current bucket Empty=1 #false if some data seen } NR==1 { if (Collect=="NF") Collect=NF } { Empty = 0; Here = Round ? round($Collect/Round)*Round : $Collect; Num[Here]++; Max = (Num[Here]>Max) ? Num[Here] : Max; } END { if (! Empty) NoSort ? histogram(Num) : sortedgram(Num) ; } function histogram(a, i) { for(i in a) print bar(a,i) } function sortedgram(a, add,i,j,keys,n) { #Pre-sort the histogram and generated the bars in sorted order for(i in a) { if (Round) { keys[j++]=i+0} #ensures numeric, not string, sort else keys[j++]=i} n=asort(keys); for(i=1;i<=n;i++) print bar(a,keys[i]); } function bar(a,i, scale) { #Genrate a single histogram bar of Marks, resized according to #the column3 width. scale = (Max < Col3) ? 1 : Col3/Max; if (Round) { return sprintf(" %" Col1".0f|%" Col2 "d| %s",\ i, a[i],str(round(a[i]*scale),"%-10s",Mark)) } else { return sprintf(" %"Col1" s|%"Col2" d| %s",\ i, a[i],str(round(a[i]*scale),"%-10s",Mark)) } }