对于扣订人员来说,用程序实现自动化流程处理是非常省事的,如同google的广告系统,它完全不需要人工干预,简直就是一部造钱的机器。

让人蛋疼的是我所做的工作本是自动化的,近期由于合作方问题,导致数据不再符合自动化统计流程,领导有命:统计数据还是要形成常态出具的,悲催的我只能领命。

对于大量的日志数据,虽不能全自动化,但也不至于完全靠手工,实现个半自动化统计程序应对短期的突发事故还是可行的

日志格式以竖线分隔,需要统计出每个位置的点击数,需求比较简单,首先考虑的是利用shell来完成。

可利用cut来截取所需字段,另shell script做数值运算也挺简单,但需要一个类似map的存储结构,位置作为key, 点击数作为value

shell没有map类型数据结构,仅有的是array,查阅了相关array使用方法后,在特定场合可以将array充当map来使用——位置当作array的下标,该位置的点击数则充当array下标的值

代码如下:

#! /bin/bash
#author sawenlee 2011-09-01
#func   calculate the sum of each position
#
PATH=.:/usr/local/bin:/bin:/usr/bin:/usr/sbin/:/sbin:/usr/local/sbin:/home/dev/bin:/usr/local/mysql/bin
export PATH
base=/data/urlred
#args length must be greater than 0
if [ $# == "0" ]; then
echo “Usage: [calc_click] src_file”
exit 1
fi
declare -a result #we need an data structure to collect results
declare -i num
maxLen=100 #set array max length
#this is the target file we are goting to handle
txtFile=$base/$1
if test -e $txtFile ; then
echo “starting to handle file : $txtFile”
echo “Index  count”
for pos in `cat $txtFile | grep success| cut -d ‘|’ -f 4` ;  #截取位置元素
do
if [ $pos -lt $maxLen -a  $pos -gt 0 ]; then   #过滤掉无效的位置
((result[$pos]++))                                            #shell script的数值运算很特别
fi
done
for  ((i = 1; i <= $maxLen; i++))                    #输出结果
do
if test ! -z “${result[$i]}”  ; then                    #如果有值才输出
echo “$i ${result[$i]}”
fi
done
else
echo “$txtFile doesn’t exist”
fi