Convert a log file to a standard csv file
The log file below has three columns separated by spaces.
2022-06-01 11:00:00 wt.nm=aa&wt.ti=t1& 2022-06-02 12:00:00 wt.nm=ab&wt.ti=t2& 2022-06-03 10:00:00 wt.nm=ac&wt.ti=t3& |
Use Java to convert the log file to a csv file with column headers: retain the first two columns; split up the 3rd column by "&" and get the string on the right of the equals sign from the first two parts to generate the new 3rd column and 4th column:
date,time,name,title 2022-06-01,11:00:00,aa,t1 2022-06-02,12:00:00,ab,t2 2022-06-03,10:00:00,ac,t3 |
Here is the SPL script:
A |
|
1 |
=file("data.log").import(;," ") |
2 |
=A1.new(#1:date,#2:time,(t=#3.split("&").(~.split("=")))(1)(2):name,t(2)(2):title) |
3 |
>file("result.csv").export@tc(A2) |
A1: Import the text file to a two-dimensional table according to the space separator.
A2: Create a new two-dimensional table by adding computed columns. #1 is the 1st column, and t(1)(2) represents the 2nd child member of the sequence’s 1st member.
A3: Write the two-dimensional table to a text file; @c option enables using the comma as the separator, and @t means importing the column headers.
Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.
Source:https://stackoverflow.com/questions/72587905/how-to-parse-log-file-to-csv-file-in-java
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