=============================================================
1、用awk实现按照电话号码进行统计
=============================================================
一般情况下,电话公司从交换机采集后将生成定长的话单文件,文件名也有一定的格式,如笔者正要处理的话单格式如下:
[zengzc@ZCBILL bill_data]$ cat Ticket_20041210_11
87881141,10,10,120, 0,20041210092920
87881142,10,30,230,-10,20041210092920
87881143,10,30,100,-20,20041210092920
87881143,10,30,110, 10,20041210093825
87881143,10,40, 90, 0,20041210095005
87881146,10,15, 45, 0,20041210092525
87881130,10,15, 80, 0,20041210092525
87881131,10,45, 60, 0,20041210090306
87881132,10,45,300, 20,20041210100101
87881133,10,60, 45, 20,20041210100101
87881134,10,10, 55,-10,20041210101010
87881134,10,20, 55,-10,20041210102020
87881143,10,20, 60, 0,20041210110201
87881143,10,10, 20, 0,20041210111515
第1~8为电话号码
第10~11为基本费
第13~14为长途费
第16~18为附加费
第20~22为优惠费
第24~27为YYYYMMDDHH24MISS格式的日期。
如果我想要把每一个客户的基本费+长途费+附加费+优惠费进行汇总,然后得出日消费总额。当然如果采用Oracle是非常容易的,先把数据用SQL LOADER导入到Oracle数据库中,然后将几个费用的字段按照电话号码进行sum就OK了。当然我要讲的是采用AIX下的awk或者gawk(Linux下,GNU)进行统计,然后导入到Oracle数据库中。我试验过,一般情况下某一个本地网每日的话单都在千万级或者亿级的记录数,在处理速度上采用awk是有优势的。以下就简单的进行介绍。
1)、先写出如下的awk程序:
[zengzc@ZCBILL bill_data]$ cat StatFee.sh
#!/bin/gawk -f
BEGIN \
{
FS=","
}
{
PhoneList[$1]=$1
AllFeesPerPhone=$2+$3+$4+$5
Fee[$1]+=AllFeesPerPhone
};
END \
{
for (Row in PhoneList)
{
print "Phone Number : "PhoneList[Row]" All Fees :"Fee[Row]
}
}
2)、chmod +x StatFee.sh
3)、执行:./StatFee.sh Ticket_20041210_11
4)、搞定,得出如下数据:
Phone Number : 87881130 All Fees :105
Phone Number : 87881131 All Fees :115
Phone Number : 87881132 All Fees :375
Phone Number : 87881141 All Fees :140
Phone Number : 87881133 All Fees :135
Phone Number : 87881142 All Fees :260
Phone Number : 87881134 All Fees :140
Phone Number : 87881143 All Fees :550
Phone Number : 87881146 All Fees :70
=============================================================
2、用awk实现按照时段进行统计
炮制电话号码的统计方法。
=============================================================
1)有如下数据(和第一部分是一样的,只是我用awk先把数据进行切分,具体切分方法见后面的附):
[zengzc@ZCBILL bill_data]$ cat Ticket_20041210_11_1
87881141,10,10,120, 0,20041210,09,2920
87881142,10,30,230,-10,20041210,09,2920
87881143,10,30,100,-20,20041210,09,2920
87881143,10,30,110, 10,20041210,09,3825
87881143,10,40, 90, 0,20041210,09,5005
87881146,10,15, 45, 0,20041210,09,2525
87881130,10,15, 80, 0,20041210,09,2525
87881131,10,45, 60, 0,20041210,09,0306
87881132,10,45,300, 20,20041210,10,0101
87881133,10,60, 45, 20,20041210,10,0101
87881134,10,10, 55,-10,20041210,10,1010
87881134,10,20, 55,-10,20041210,10,2020
87881143,10,20, 60, 0,20041210,11,0201
87881143,10,10, 20, 0,20041210,11,1515
2)编写awk脚本,并且存储为:StatFee_Time.sh
[zengzc@ZCBILL bill_data]$ cat StatFee_Time.sh
#!/bin/awk -f
BEGIN \
{
FS=","
}
{
TimeList[$7]=$7
AllFeesPerPhone=$2+$3+$4+$5
Fee[$7]+=AllFeesPerPhone
};
END \
{
for (Row in TimeList)
print "Time Section : "TimeList[Row]" All Fees :"Fee[Row]
}
3)、使得脚本StatFee_Time.sh可执行
chmod +x StatFee_Time.sh
4)执行 ./StatFee_Time.sh Ticket_20041210_11_1,OK,得到结果:
[zengzc@ZCBILL bill_data]$ ./StatFee_Time.sh Ticket_20041210_11_1
Time Section : 09 All Fees :1110
Time Section : 10 All Fees :650
Time Section : 11 All Fees :130
附:切分话单中的日期的脚本:
awk -F, '{print $1","$2","$3","$4","$5","substr($6,1,8)","substr($6,9,2)","substr($6,11,4)}' Ticket_20041210_11 >Ticket_20041210_11_1