linux awk实现二维数组遍历的方法,用到了awk字段分隔符与awk循环语句,感兴趣的朋友参考下。 awk以二维数组方式处理数据,一个文件几十万行,用awk命令分析效率还是很高的。
日志结构:
文件内容:
Jul 13 23:59:58 [info] {SPR}poker{SPR}20100713235958{SPR}39487249{SPR}S1{SPR}
Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}40321910{SPR}S5{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}45937395{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46047210{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46100689{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}mcsd{SPR}20100713235958{SPR}46144879{SPR}S22{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46193512{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46249154{SPR}S9{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46311452{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46361704{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46401796{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}46412612{SPR}S10{SPR} Jul 13 23:59:58 [info] {SPR}dfh{SPR}20100713235958{SPR}46414008{SPR}7{SPR} Jul 13 23:59:58 [info] {SPR}dandan{SPR}20100713235958{SPR}6428766{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}mcsd{SPR}20100713235959{SPR}26810901{SPR}S1{SPR} Jul 13 23:59:59 [info] {SPR}wulin{SPR}20100713235959{SPR}28006063{SPR}hero10.wan.360.cn{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}29498036{SPR}S2{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}30401399{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}plsm{SPR}20100713235959{SPR}33290378{SPR}S3{SPR} Jul 13 23:59:59 [info] {SPR}wulin{SPR}20100713235959{SPR}35130674{SPR}hero14.wan.360.cn{SPR} Jul 13 23:59:59 [info] {SPR}mcsd{SPR}20100713235959{SPR}35990716{SPR}S22{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}39135264{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}39263733{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}45902989{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}46051567{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}46237264{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}46241464{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}46320919{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}46337146{SPR}S10{SPR} Jul 13 23:59:59 [info] {SPR}dandan{SPR}20100713235959{SPR}46411556{SPR}S10{SPR}
例1:
#将字段按{SPR}分隔
BEGIN{FS="{SPR}";OFS="\t"} { name=$2; #用三止运算符,将gamearr数组里KEY为游戏名,结果为换行符加上用户ID gamearr[name]=gamearr[name](gamearr[name]?"\n":"")$4; } END{ #循环每个游戏 for (name in gamearr) { #各个游戏对应的文件名 filename = name".log"; print gamearr[name] > filename; } }
例2:
复制代码代码示例:
#将字段按{SPR}分隔
BEGIN{FS="{SPR}";OFS="\t";} { #arrgame数组的KEY是游戏 arrgame[$2]++; #gameqid数组的KEY是游戏,用户ID gameqid[$2,$4]++; } END{ #循环每个游戏 for(name in arrgame) { filename = name".log"; for(qid in gameqid) { #将游戏名与用户ID分隔开,存放在qidarr数组中 split(qid,qidarr,SUBSEP); if(name == qidarr[1]){ print qidarr[2] > filename; } } } } |