多文件内容排序后合并

【问题】

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

For ex:

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 that filename in the beginning of each line like mentioned above. Could anyone help me figure out how to append the file name?

【回答】

将 N 个文件分别按行读入,并各加一列“文件名”,再做纵向合并,形成两个字段的二维表。按第 1 个字段(原字符串)排序,再整理成“文件名 – 原字符串”,最后输出到文件。

JAVA 缺乏相关类库,实现麻烦。可用 SPL 辅助实现再集成,代码简单易懂:


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)


上述代码很容易集成到 JAVA,可参考 JAVA调用SPL脚本