Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中检查文件中的重复内容#_C# - Fatal编程技术网

C# 在C中检查文件中的重复内容#

C# 在C中检查文件中的重复内容#,c#,C#,我有两个或多个日志文件,它们将合并到一个新文件中 日志文件格式可以是 等等 我的要求如下 检查每个日志文件中每行的格式是否正确,即记录所有值 检查是否没有重复项 验证文件是否正确合并,即每个日志文件中的所有日志行都已合并到新日志文件中 将新合并的文件与基线文件进行比较 我已经为1编写了代码。我读取文件并按行/列将内容加载到数据集中 data.Tables[tableName].Columns.Add("Dir"); data.Tables[tableName]

我有两个或多个日志文件,它们将合并到一个新文件中

日志文件格式可以是

等等

我的要求如下

  • 检查每个日志文件中每行的格式是否正确,即记录所有值
  • 检查是否没有重复项
  • 验证文件是否正确合并,即每个日志文件中的所有日志行都已合并到新日志文件中
  • 将新合并的文件与基线文件进行比较
  • 我已经为1编写了代码。我读取文件并按行/列将内容加载到数据集中

            data.Tables[tableName].Columns.Add("Dir");
            data.Tables[tableName].Columns.Add("Path1");
            data.Tables[tableName].Columns.Add("Path2");
    
            using (StreamReader reader = new StreamReader(log))
            {
                string line = string.Empty;
                while ((line = reader.ReadLine()) != null)
                 {
                     data.Tables[tableName].Rows.Add(line.Split(new string[] { "\t" }, data.Tables[tableName].Columns.Count, StringSplitOptions.RemoveEmptyEntries));
                 }
            }
    
    但为了完成其余的任务,我不确定将这些行加载到数据集中是否正确?最快和更好的方法是什么?我可以循环每一行的值并与其他行进行比较,但我不认为它会更快。 日志文件的大小可以在20-45MB之间

    合并日志内容应如下所示(行可以按任何顺序排列)


    感谢您的查看。

    如果您可以将所有数据一次加载到内存中,那么检查重复项就很容易了:只需加载数据并让LINQ删除重复项即可。即:

    List<string> lines = LoadEverything();
    foreach (line in lines.Distinct()) // might want to supply an equality comparer
    {
        // write line to output file
    }
    
    或者,如果您只是想知道是否存在任何重复项:

    if (dupes.Any())
        Console.WriteLine("There are duplicates!");
    

    如果人们能看到你的代码,你会得到更多的帮助。为什么不在你的问题中发布代码呢?你能正确地定义合并吗?因为有很多方法可以定义合并,用我的代码更新这种数据,将日志解析到DataTable中。在endAArgh添加了合并定义。我添加了代码,但我不知道为什么它现在消失了。再加一次。
    Dir1 File1Path1 File1Path2 Timestamp tempfileName
    Dir1 File2Path1 File2Path2 Timestamp tempfileName
    Dir2 File1Path1 File1Path2 Timestamp tempfileName
    Dir4 File1Path1 File1Path2 Timestamp tempfileName
    Dir3 File1Path1 File1Path2 Timestamp tempfileName
    Dir3 File2Path1 File2Path2 Timestamp tempfileName
    Dir3 File1Path1 File1Path2 Timestamp tempfileName
    
    List<string> lines = LoadEverything();
    foreach (line in lines.Distinct()) // might want to supply an equality comparer
    {
        // write line to output file
    }
    
    var dupes = 
        lines.GroupBy(l => l)
             .Select(g => new { Value = g.Key, Count = g.Count() })
             .Where(g => g.Count > 1);
    foreach (var d in dupes)
    {
        Console.WriteLine("'{0}' is a dupe.", d.Key);
    }
    
    if (dupes.Any())
        Console.WriteLine("There are duplicates!");