Get Columns by Their Positions

 

Question

I have a text file which has all the information I need in lines and this needs to be converted into a .csv file.

Example:

abbccccdeffffiiiiiiiiiiiiiijjkkkkkkkkkkkllmmmmmmnnooo

abbccccdeffffiiiiiiiiiiiiiijjkkkkkkkkkkkllmmmmmmnnooo

abbccccdeffffiiiiiiiiiiiiiijjkkkkkkkkkkkllmmmmmmnnooo

abbccccdeffffiiiiiiiiiiiiiijjkkkkkkkkkkkllmmmmmmnnooo

abbccccdeffffiiiiiiiiiiiiiijjkkkkkkkkkkkllmmmmmmnnooo

abbccccdeffffiiiiiiiiiiiiiijjkkkkkkkkkkkllmmmmmmnnooo

 

So basically

·                     1.“a” is first column with just one char

·                     2.“bb” is second column with length 2

·                     3.“cccc” is third column with length 4

·                     4.“d” is fourth column with length 1

·                     5.“e” is fifth column with length 1

·                     6.“ffff” is sixth column with length 4

As we can see from this example, I cannot use a delimiter with space or comma. They are all of different lengths. Please point me to the right direction. I just need an idea of how to approach to this problem.

 

Answer

It’s simple and easy to understand to get columns in SPL (Structured Process Language):

A

1

=file("d:\\source.csv").import@si()

2

=A1.new(mid(~,1,1),mid(~,2,2),mid(~,4,4),mid(~,8,1),mid(~,9,1),mid(~,10,4))

 

A1: Import source.csv.

A2: mid(s, start{, len}) gets a substring of a source string to split each of A1’s lines into a two-dimensional table. The final result is also a two-dimensional table over which you can perform sorting, grouping & aggregation or other queries.

An SPL script can be easily integrated with a Java application. See How to Call an SPL Script in Java to learn more.