Union & Sort

Question

I have to merge different files into one according to timestamp. I have succeeded in sorting them and bringing them to one single file. In order to know where the lines come from (from which file), I am trying to add the original file name at the beginning of each line.

For example:

File1:

 

12:23:21,234 some text

 

13:23:21,234 some text

 

17:45:12,576 some text

 

File2:

 

15:23:21,234 some text

 

15:28:01,254 some text

 

Merged file:

 

File1 \- 12:23:21,234 some text

 

File1 \- 13:23:21,234 some text

 

File2 \- 15:23:21,234 some text

 

File2 \- 15:28:01,254 some text

 

File1 \- 17:45:12,576 some text

 

I am trying to add the file name at the beginning of each line like mentioned above. Could anyone help me figure out how to append the file name?

 

Answer

Read in the two files respectively line by line, add a filename column to each of them, union them into a 2D table, sort the table by the first field (strings coming from the source text), switch over the positions of the two fields (filename,sourceStrings), and, finally, write the data into a specified text file.

It’s complicated to do this in Java because the language doesn’t have related class libraries. We can handle this first in SPL and then integrate the script with the Java application (See How to Call an SPL Script in Java). The SPL code is simple and easy to understand:

A

1

=["File1","File2"]

2

=A1.conj(file("D:\\"+~+".txt").import@s().derive(A1.~))

3

=A2.sort(#1).(#2+"-"+#1)

4

=file("D:\\result.txt").write(A3)