难以理解AWK语法 [英] Difficulty understanding AWK syntax
本文介绍了难以理解AWK语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我承认我对AWK完全陌生,不知道如何使用它,但是我正在尝试根据GNU awk用户指南编写代码示例.
So I will admit I am completely new to AWK and have no idea how to use it, however I am trying to work off a code sample from the GNU awk user guide.
{
for (i = 1; i <= NF; i++)
freq[$i]++
}
END {
for (word in freq)
printf "%s\t%d\n", word, freq[word]
}
此代码段仅显示频率中存在的每个单词的计数
This code snippet simply prints the count of every word present in the in freq
{
data[++data_index] = $0
}
END {
produce_numbers(data)
for(i in freq)
printf("%s\t%d", i, freq[i])
}
function produce_numbers(sortedarray)
{
n = asort(sortedarray)
sum = 0
for(i = 1; i <= n; i++)
{
freq[$i]++
}
return
}
这是我要使用的.我确信它有几个错误(显然是因为我的任何输出是错误的或不存在的),但是我对语法的了解不足.另外,我要传递的文件是这样的
This is one I am trying to use. I am sure it has several mistakes (Obviously because any output I have is wrong or nonexistant) but I am not experienced enough to know the syntax. Also the file I am trying to pass is something like this
A
B
A
C
B
A
C
D
推荐答案
我重新组织并修复了该问题:
I reorganized and fixed that:
function produce_numbers(sortedarray)
{
n = asort(sortedarray)
# sum = 0 # not used
for(i = 1; i <= n; i++)
{
freq[sortedarray[i]]++ # $i is unusable at this point
}
return
}
{
data[++data_index] = $0
}
END {
produce_numbers(data)
for(i in freq)
printf("%s\t%d\n", i, freq[i]) # added newline to output
}
输出:
A 3
B 2
C 2
D 1
这篇关于难以理解AWK语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文