How should you write an awk array code?
In AWK, arrays do not need to be declared and can be used directly. Here is an example:
# 使用数组统计每个单词的出现次数
{
for (i = 1; i <= NF; i++) {
words[$i]++
}
}
# 输出统计结果
END {
for (word in words) {
print word, words[word]
}
}
In the example above, ‘words’ is an array used to count the frequency of each word. Within each line of processing, a loop is defined to iterate through each word, and increment the count of that word using ‘words[$i]++’. In the final END block, a ‘for (word in words)’ loop is used to iterate through each element in the array and output the corresponding word and count.