【AWK】Scanning all Elements of an Array--每日一译2013-02-05
来源:互联网 发布:易语言打码挂机源码 编辑:程序博客网 时间:2024/06/05 07:39
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拥有一个指定的遍历数且的语句如下:
body
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
- 【AWK】Scanning all Elements of an Array--每日一译2013-02-05
- Basic Example of an Array OF AWK 【每日一译】--2013-01-27
- Find all max of elements of an array
- Find all max of elements of an array
- JSP for loop output all elements of an array
- index in array-AWK碎片【每日一译】--20130203
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- Interview Return Products of All Other Elements in Array
- awk零碎1【每日一译】--2013-02-01
- Learning Perl: 3.1. Accessing Elements of an Array
- get an array of arrays with unique elements
- Given an array of integers, sort the array according to frequency of elements
- Find the two non-repeating elements in an array of repeating elements
- Looking for all keys of an multi dimensional array
- Given an array of numbers, return array of products of all other numbers (no division)
- The minimum number of elements to reach the end of an array
- Matching Portions of A String to Elements of an Array in Ruby
- [Facebook] Products of all elements
- ACM Rank Table(poj 2397)
- NYOJ 264题 国王的魔镜
- NS3中日志信息输出到文件
- Cities
- ASP.NET MVC3.0中同一View如何返回多个Model或数据集
- 【AWK】Scanning all Elements of an Array--每日一译2013-02-05
- SVN中的Import
- 实现设计模式:工厂模式
- Android 内存优化
- 织梦当前位置不显示最后一个符号
- Android混淆注意事项
- HDOJ 4576
- NAND FLASH扇区管理及初始化
- EOS开发五