【AWK】Scanning all Elements of an Array--每日一译2013-02-05

来源:互联网 发布:易语言打码挂机源码 编辑:程序博客网 时间:2024/06/05 07:39
In programs that use arrays, often you need a loop that executes once for each element of an
array. In other languages, where arrays are contiguous and indices are limited to positive integers,
this is easy: the largest index is one less than the length of the array, and you can FI nd all the valid
indices by counting from zero up to that value. This technique won't do the job in awk, since any
number or string may be an array index. So awk has a special kind of for statement for scanning

an array:

在使用数组的程序中,对于数组中的每个元素通常需要循环一次执行一次。在其它语言中,数组是连

续以及它的索引条目是正整数,很简单的:最大的索引条目比数组的长度小1,并且你可以所有的可用

条目通过从0计算起到刚的那个值(索引最大条目减1)。这个技术在AWK中是不做此工作的,因为任何

的数字或者字符串都可以作为索引的条目。所以AWK拥有一个指定的遍历数且的语句如下:


for (var in array)

body



This loop executes body once for each di ferent value that your program has previously used as an
index in array, with the variable var set to that index.

这个循环执行体对于不同的值每次执行一次,这些值是前面程序中在数组中作为索引条目来使用的,

使用变量VAR来设置索引的条目。

Here is a program that uses this form of the for statement. The FIrst rule scans the input records

and notes which words appear (at least once) in the input, by storing a 1 into the array used with

the word as index. The second rule scans the elements of used to nd all the distinct words that
appear in the input. It prints each word that is more than 10 characters long, and also prints the
number of such words. See Chapter 11 [Built-in Functions], page 89, for more information on the
built-in function length.

这个程序语句使用这种格式。它第一个规则扫描输入记录并且记录了哪些输入的单词至少出现过一次,

通过存储1在数组中,使用单词来作为索引条目。它输出每个单词,它的长度是大于10的单词,并且

打印此类单词的数量。


# Record a 1 for each word that is used at least once.
{
for (i = 1; i <= NF; i++)    #每行的每列遍历一次,然后自动往下一行
used[$i] = 1
}
# Find number of distinct words more than 10 characters long.
END {
for (x in used)   #注意X为索引条目,不是值
if (length(x) > 10) {
++num_long_words
print x   输出索引条目,其实也就是我们要的内容
}
print num_long_words, "words longer than 10 characters"
}


例子:

[oracle@localhost ~]$ awk -f awkf  zbk_data
eeeeeeeeeee
zhangwiangjash
ccccedfasd
aaaaaaaaaa
dddddddddd
bbbbbbbbbbb
6 words longer than 10 characters
[oracle@localhost ~]$ cat zbk_data
aaaaaaaaaa  bbbbbbbbbbb ccccccccc
dddddddddd  eeeeeeeeeee ghghghghg
ccccedfasd  zhangwiangjash  goafsced
aaaaaaaaaa  bbbbbbbbbbb ccccccccc
aaaaaaaaaa  bbbbbbbbbbb ccccccccc
aaaaaaaaaa  bbbbbbbbbbb ccccccccc
aaaaaaaaaa  bbbbbbbbbbb ccccccccc












原创粉丝点击