How to Use Hash Map to Count the Frequencies of Values and Itera
- 时间:2020-10-09 18:35:39
- 分类:网络文摘
- 阅读:113 次
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
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
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:营养盘点:各种米饭的神奇养生功效 预防酒糟鼻的饮食疗法有哪些? 教大家自制四款降火粥给身体降火 适当吃些辣椒的6大保健养生功效 保健养生:盘点草莓的六大养生功效 春天最好少吃这几种反季节水果 牛奶和酸奶,到底哪个更有营养呢? 健康与饮食:养胃护胃之八大饮食禁忌 关于饮用牛奶饮品的六个健康误区 不同颜色的玉米其营养价值也各不相同
- 评论列表
-
- 添加评论