Chinaunix
标题:
请教一个AWK的语法问题
[打印本页]
作者:
Siete
时间:
2008-10-09 14:20
标题:
请教一个AWK的语法问题
在看《LINUX与UNIX SHELL编程指南》关于AWK一章的时候,有点疑问
在讲解AWK数组的时候有这么个例子:
$vi group_student
1 yellow#junior
2 orange#senior
3 yellow#junior
4 purple#junior
5 brown-2#junior
6 white#senior
7 orange#senior
8 red#junior
9 brown-2#senior
10 yellow#senior
11 red#junior
12 blue#senior
13 green#senior
14 purple#junior
15 white#junior
复制代码
awk脚本
$vi belts.awk
1 #!/bin/awk -f
2 BEGIN{FS="#"
3 belt["yellow"]
4 belt["orange"]
5 belt["red"]
6 student["junior"]
7 student["senior"]
8 }
9 {for (colour in belt)
10 {if ($1==colour)
11 belt[colour]++}}
12 {for (senior_or_junior in student)
13 {if ($2==senior_or_junior)
14 student[senior_or_junior]++}}
15 END{ for (colour in belt) print "The club has", belt[colour], colour, "belts"
16 for (senior_or_junior in student) print "The club has", student[senior_or_junior], senior_or_junior, "students"}
复制代码
最后用命令输出
$ belts.awk grade_student
The club has 2 orange belts
The club has 2 red belts
The club has 3 yellow belts
The club has 7 senior students
The club has 8 junior students
复制代码
请高人指点一下这里for语句里or左右两边下划线的用法。
作者:
ly5066113
时间:
2008-10-09 14:23
senior_or_junior 只是一个自定义的变量,可以换成任何你想用的。
作者:
Siete
时间:
2008-10-09 14:29
再仔细看看是那么回事,是自己想复杂了。
谢谢了。
作者:
xwmhmily
时间:
2008-10-09 20:44
无特别意思,变量名中的一部分
作者:
sea_soft
时间:
2008-10-28 16:21
谢谢楼主,正好练习awk,懒得输入,这下可以直接COPY一份了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2