awk 数组应用案例
一、四个案例解析
1.1 案例1 查酒店
假设我们有一个酒店:
酒店<===>hotel
酒店里面有几个房间110,119,120,114,401
酒店的110房间<===> hotel[110]
酒店的119房间<===> hotel[119]
酒店的120房间<===> hotel[120]
如何查看房间里住的是哪位客人?
# awk 'BEGIN{hotel[110]="tom"; hotel[119]="jack";hotel[121]="owen";print hotel[110],hotel[121],hotel[119]}'
tom owen jack
使用for语句,对酒店进行循环/查房
# awk 'BEGIN{hotel[110]="tom"; hotel[119]="jack";hotel[121]="owen";
> for(i in hotel)
> print i,hotel[i]
> }'
119 jack
110 tom
121 owen
i 为房间号码
hotel[i] 房间里所住的人
1.2 案例 统计域名访问次数(去重统计)
准备源文件
# cat > www.txt <<eof
http://www.aiops.com/index.html
http://www.aiops.com/1.html
http://post.aiops.com

最低0.47元/天 解锁文章
682

被折叠的 条评论
为什么被折叠?



