String Split and Group over a Text File

Question

I have a txt file:

 

[className1]   Text1

 

[className1]   Text2

 

[className 2]   Text 3

 

I want to write the file to Oracle 10g.
Requirements: 1. The square brackets won’t be written into the database; 2. [className] and Text should be separated by space, and rows are separated by spacebar-generated space; 3. Records having same className will be recorded as one record in the database with texts appended.

 

Answer

You can hardcoded the process in Java. Or you can hack it in SPL (Structured Process Language) effortlessly. The SPL script can be easily integrated with a Java application. Learn more about Java invocation here: How to Call an SPL Script in Java. Here’s the SPL script:

A

1

=file("E:\\s.txt").import@i()

2

=A1.select(~).(~.split@t(" ")).new(left(mid(~(1),2),-1):Class,~(2):Text)

3

=A2.group(Class).new(~.Class,~.(Text).   concat@c():Text)

4

>db.update(A3,tableName,col1:className,col2:Text)

A1: Import text file s.txt as a sequence;

 

Index

Member

1

[className1] Text1

2

(null)

3

[className1] Text2

4

(null)

5

[className2] Text3

6

(null)

 

A2: Remove empty lines, split each sequences and create a table sequence consisting of two fields - Class and Text, and then delete square brackets enclosing Class values;

 

Index

Class

Text

1

className1

Text1

2

className1

Text2

3

className2

Text3

 

A3: Group records by Class and join up texts under same class name;

 

Index

Class

Text

1

className1

Text1,Text2

2

className2

Text3

 

A4: Write the result into database db. The database connection is already configured.