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


    【问题】

    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,具体代码如下:

    AB
    1=file("E: \\webdata.log").cursor@c()=file("e:\\result.txt")
    2for 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:增加一行数据,并计算A2第2列的平均值。

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

     

  • 相关阅读:
    leetcode题107二叉树的层序遍历-日记篇
    JS实现简易观察者模式
    go语言基本操作---三
    二叉树【Java】
    【科学文献计量】科学文献知识网络分析基础
    flink之Sink to MySQL和Redis
    2024最新的,免费的 ChatGPT 网站AI(八个)
    Linux系统调优介绍
    Python入门教程 | Python 函数与参数
    计算机毕业设计springboot+vue+elementUI 广场舞团高校舞蹈社团管理系统
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/127102035