Group a File & Write Groups to Multiple Files
【Question】
I have a text file named text.txt:
09102306614,2,104
09106148895,2,104
09102567410,3,104
,...
09122586173,2,104
09126413664,2,104
09125554671,3,104
,...
I want to read text.txt with Java and create text1.txt from first column data where numbers with same first four characters are grouped together, for example, text1.txt includes:
0910*******
0910*******
0910*******
,...
And text2.txt includes:
0912*******
0912*******
0912*******
,...
【Answer】
Get the first field, group it by the first four characters, and write each group to a file. It’s roundabout to code it in Java. But it’s simple to do it in SPL (Structured Process Language). The SPL script can be easily integrated with a Java application (See How to Call an SPL Script in Java).
A |
|
1 |
=file("text.txt").import@ic(#1:string) |
2 |
=A1.group(left(~,4)).run(file("d:\\text"+string(#)+".txt").export(~)) |
A1: Read in the first column and convert it to a string.
A2: Group the column data by the first four characters and write each group to a target file.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
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