- 论坛徽章:
- 0
|
在看《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左右两边下划线的用法。 |
|