2016-04-22 11 views
0

私は、ファイル内のいずれかの列からタイムスタンプでソートする必要がある2つのプレーンテキストファイルを持っています。 output_File.txt2つのテキストファイルをTimeStampでマージする

sample_A.txt

05:37:58 01:00 0 0  |05:32:00|93328|5352|   |Description 1 
05:43:58 01:00 0 0  |05:49:40|99256|5357|   |Description 2 
05:44:58 00:30 0 0  |05:50:40|99301|5358|   |Description 3 

sample_B.txt

04:58:11 00:02 0 0  |04:58:55|58787|0335|   |Description A 
04:58:12 01:01 0 0  |04:59:00|57701|0336|   |Description B 
06:09:37 01:00 0 0  |06:10:00|58181|0348|   |Description C 

(これはファイルをマージした後どのように見えるかである)

04:58:11 00:02 0 0  |04:58:55|58787|0335|   |Description A 
04:58:12 01:01 0 0  |04:59:00|57701|0336|   |Description B 
05:37:58 01:00 0 0  |05:32:00|93328|5352|   |Description 1 
05:43:58 01:00 0 0  |05:49:40|99256|5357|   |Description 2 
05:44:58 00:30 0 0  |05:50:40|99301|5358|   |Description 3 
06:09:37 01:00 0 0  |06:10:00|58181|0348|   |Description C 

タイムスタンプは、例えば最初の8列、次のとおりです。これは私が持っているコードですが、それが唯一の区切り文字を使用して、同じ行にファイルを追加し、することができます

05:37:58 
05:43:58 
05:44:58 

:ファイルsample_A.txtオン 私は助けをお願いしますか?

ありがとうございました。

string[] files = new string[] { @"c:\temp\sample_A.txt", @"c:\temp\sample_B.txt" }; 
var hash = new Dictionary<string, Dictionary<string, bool>>(); 
foreach (var file in files) 
{ 
    string[] fileContents = File.ReadAllLines(file); 
    foreach (string line in fileContents) 
    { 
     string[] a = line.Split('|'); 
     if (!hash.Keys.Contains(a[0])) 
      hash[a[0]] = new Dictionary<string, bool>(); 
     hash[a[0]][a[1]] = true; 
    } 
} 
foreach (var key in hash.Keys) 
    Console.WriteLine(key + "," + string.Join(",", hash[key].Keys.ToArray())); 

答えて

1

私はあなたが単純化できると思います。ここにはどんな種類のユニークさが必要ですか?

もしそうでなければ、すべてのファイルを読み込んで一つの大きなリストにマージし、ソートしてソートされたバージョンを出力することができますか?

string[] files = new string[] { @"c:\temp\sample_A.txt", @"c:\temp\sample_B.txt" }; 
var merged = new List<string>(); 
foreach (var file in files) 
{ 
    string[] fileContents = File.ReadAllLines(file); 
    Collections.addAll(merged, fileContents); 
} 

Collections.sort(merged); 
foreach (string line in merged) 
{ 
    Console.WriteLine(line); 
} 

あなただけのタイムスタンプの最初の8つの文字を比較する必要がある場合、あなたはsortメソッドにカスタムコンパレータを渡す+作成することができますか?

時間の一意性が必要な場合は、リストがソートされたら、最後のアイテムと現在(または現在および次)を見て、タイムスタンプが同じであればアイテムの書き込みをスキップできます。

+0

ありがとう、これは実際に動作します! – AJ152