比较.txt文件并获取差异

本文关键字:获取 txt 文件 比较 | 更新日期: 2023-09-27 18:16:04

我试图比较两个文本文件a.txtb.txt,我想得到两者之间的差异。
a.txt是昨天的结果。
b.txt为当前结果。
棘手的事情是,我想找出"b.t txt"与"a.t txt"相比缺少什么,即使可能在"b.t txt"中添加了一些新对象,这些新对象需要被排除。
这两个文件没有排序,所以'a.t txt'中索引1中的内容可以是'b.t txt'中的索引2。我正在比较像"mano - mathias rønnow nørtoft"这样的字符串。
我所尝试的只是最终显示新对象。
我试过的:

 string[] File1Lines = File.ReadAllLines(path);
 string[] File2Lines = File.ReadAllLines(newPath);
 List<string> NewLines = new List<string>();
for (int lineNo = 0; lineNo<File1Lines.Length; lineNo++)
  {
    if (!String.IsNullOrEmpty(File1Lines[lineNo]) && !String.IsNullOrEmpty(File2Lines[lineNo]))
    {
      if(String.Compare(File1Lines[lineNo], File2Lines[lineNo]) != 0)
        NewLines.Add(File2Lines[lineNo]) ;
    }
    else if (!String.IsNullOrEmpty(File1Lines[lineNo]))
    {
    }
    else
    {
      NewLines.Add(File2Lines[lineNo]);
    }
  }
  if (NewLines.Count > 0)
  {
    File.WriteAllLines(resultpath, NewLines);
  }

这只是给我合并的文件。希望我的解释正确。

尝试了这个,为什么不工作?

        List<string> a = File.ReadAllLines(path).ToList();
        List<string> b = File.ReadAllLines(newPath).ToList();
        List<string> copy = new List<string>(a);
        foreach (string s in copy)
        {
            if (b.Contains(s))
            {
                a.Remove(s);
            }
            else
            {
                continue;
            }
        }
        myWriter.WriteLine(a);

比较.txt文件并获取差异

这取决于你想要的diff有多精确和多快。

一个简单的实现是获取A和B的所有行,对于A中的每一行,如果B包含该行,则从A和B中删除该行一次。剩下的将是A中的线,而不是B中的线,反之亦然。

注意这个方法不考虑排序,所以

Log 1  
C   
B   
A

Log 2  
A  
B   
C

被认为是相同的。

List<string> A;
List<string> B;
List<string> aCopy = new List(A);
foreach(string s in aCopy)
{
    if (B.Contains(s))
    {
        A.Remove(s);
        B.Remove(s);
    }
}
//Whats in A are whats missing in B
//Whats in B are whats missing in A

您可以使用regex命令连接,排序和删除相等字符串

 using System;
    using System.Text;

     using System.Text.RegularExpressions;
   class Program

{
 static void Main()
{
    string strFile4xf = File.ReadAllText(@"a.txt");
    strFile4xf = Regex.Replace(    strFile4xf,     @"(.*?)'r", "$1a'r");
   File.WriteAllText(@"a1.txt", strFile4xf);

    string strFile4xe = File.ReadAllText(@"b.txt");
      strFile4xe = Regex.Replace(    strFile4xe,     @"(.*?)'r", "$1b'r");
   File.WriteAllText(@"b1.txt", strFile4xe);


        string s4 = File.ReadAllText(@"a1.txt"); 
   string s2 = File.ReadAllText(@"b1.txt"); 
  string sn = string.Concat(s4, s2);
  File.WriteAllText(@"join.txt", sn);
  var contents = File.ReadAllLines("join.txt");
       Array.Sort(contents);
    File.WriteAllLines("join.txt", contents);
     string strFile4x = File.ReadAllText(@"join.txt");
   strFile4x = Regex.Replace(    strFile4x,     @"'n(.*?)a'r'n'1b'r", "");
     File.WriteAllText(@"removeequal.txt", strFile4x);


   var contents2 = File.ReadAllLines("removeequal.txt");
       Array.Sort(contents2);
    File.WriteAllLines("removeequal.txt", contents2);


string strFile4x2 = File.ReadAllText(@"removeequal.txt");
 strFile4x2 = Regex.Replace(    strFile4x,     @"'n'r", "");
 File.WriteAllText(@"blanklines.txt", strFile4x2);

     }
  }

这个命令匹配重复字符串'n(.*?)'r'n'1'r当这个排序