多文件内容排序后合并

【问题】

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脚本

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值