How to Use Hash Map to Count the Frequencies of Values and Itera

  • 时间:2020-10-09 18:35:39
  • 分类:网络文摘
  • 阅读:85 次

AWK is a powerful text-processing programming language. Given a multi-million lines of text file containing the following data – we want to know the frequencies the delegation at each integer interval e.g. 2, 3, 4:

awk-data-example-steem How to Use Hash Map to Count the Frequencies of Values and Iterate the Key-Value Pairs in AWK? awk

awk-data-example-steem

First, we output the text file to console using cat then pipe it into grep to filter out non-data rows, and then we can execute the awk script.

cat steem3.txt | grep "delegates" | awk '$6 > 0 {
   data[int($6)]++
} 

END { 
  for (sp in data) {
     print (sp, "=", data[sp]);
  } 
}' 

It filters out the records that have zero values (undelegation records) – then we round the fraction numbers into integers and count them in a hash map.

Basically, we don’t have to declare the hash table prior to using it. And we can access it using the syntax map[key]. And at the END section, we can iterate the keys in the hash map in awk and print each value:

for (key in map) {
   print ("key is ", key, ", value is ", map[key]);
}
awk How to Use Hash Map to Count the Frequencies of Values and Iterate the Key-Value Pairs in AWK? awk

awk

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
五年级感恩父亲节作文300字  三年级感恩父亲节作文200字  清明节扫烈士墓  清明·祭祀·缅怀  文化艺术节  关于报刊亭的数学题  数学题:当甲车到达时,乙车距离工地还有24千米  数学题:光华小学中、高年级共有学生600名  数学题:求大阴影部分的面积  数学题:如何只用这2个水壶从池塘里取得3升的水 
评论列表
添加评论