Insert Aggregates between Data Groups
【Question】
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
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 (put values together, run the function that spits back an average, and put it in the right place), but I can't figure out how to write a loop that takes input until a variable is different and put that into the check ("look at webcat until you see something else, then run this function again with webroot")
【Answer】
To insert aggregates between grouped data, we read in data until the grouping field value changes and write the current group of data and the aggregate to a target file, and then go on to read the next group. We can hardcode the algorithm but the process is roundabout. So, I choose to express it in SPL (Structured Process Language):
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: Read in the comma-separated content from webdata.log and return a cursor.
A2-B4: Retrieve the first column in A1 to perform the desired computations.
A2: Read in records until a new value of the first column appears (that is, read in records where the first column value is webcat_service and then those where the first column value is webroot_service).
B2: Write the current group in A2 to result.txt.
B3: Add a record in the desired format and calculate the average A2’s second column values.
B4: Append the new record to result.txt.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL