【问题】
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脚本 】
该问题涉及到文件处理,具体是将多个文件按照时间戳排序后合并到一个文件中,并在每行开头添加原始文件名作为标记。已成功按时间顺序整合文件,现在需要在每行前插入对应文件名。提供的示例显示了如何在输出中格式化这一信息。解决方案可能涉及读取每个文件,添加文件名前缀,然后按时间戳排序并写入新文件。
686

被折叠的 条评论
为什么被折叠?



