文本分析,组间插入汇总值

【问题】

Ok, so I'm trying to read a log file and parse useful data out of it.
it looks kind of like
webcat_service,1.0,50
webcat_service,1.5,30
webcat_service,2.5,10
webcat_service,4.0,5
webroot_service,10.0,50
webroot_service,15.0,20
webroot_service,20.0,10
webroot_service,30.0,5
at this point.
Now, what I want is to split that off into 
webcat_service_1.0,50
webcat_service_1.5,30
webcat_service_2.5,10
webcat_service_4.0,5
webcat_service_avg, $FOO
webroot_service,10.0,50
webroot_service,15.0,20
webroot_service,20.0,10
webroot_service,30.0,5
webroot_service_avg, $BAR
I can do the easy part (putting the values together, running the function that spits back an average, and puts it in the right place), but I can't figure out how to write a loop that takes input until a variable is different, then put that into the check ("look at webcat until you see something else, then run this function again with webroot")

【回答】

       在已经分组的数据间插入汇总值,通常的做法是依次读入本组数据,直到数据发生变化,然后将本组数据和汇总值追加到新文件中,再读入下一组数据。可以用硬编码实现上述算法,但过程有些麻烦,这种情况下可以考虑用SPL,具体代码如下:


A

B

1

=file("E: \\webdata.log").cursor@c()

=file("e:\\result.txt")

2

for A1;_1

=B1.export@ac(A2)

3


=A2._1+"_avg,"+string(A2.avg(_2))+"\r\n"

4


=B1.write@a(B3)

 

A1:读取逗号分隔的webdata.log中的内容,结果返回游标。

A2-B4:循环读取A1第一列内容,并进行计算。

    A2:每次从A1读取部分记录至第一列数据有变化(即先读取第一列为webcat_service的数据,再读取第一列为webroot_service的数据)

    B2:先将A2追加导入到result.txt

    B3:增加一行数据,并计算A22列的平均值。

    B4:将增加的行追加写入result.txt