简体   繁体   English

比较两个文本文件C#时如何跳过缺失的行和多余的行

[英]How to skip missing lines and extra lines when compare two text files C#

I need to compare two txt files (file1 is used as baseline, and file2 is the one needs to compare with file1), I want to find out the differences to the file3, including missing lines, extra lines, and the lines with different content. 我需要比较两个txt文件(file1用作基线,file2是需要与file1进行比较),我想找出与file3的差异,包括缺少的行,额外的行和不同内容的行。

For my current code, if 2nd line in file2 is missing, all of following lines in file2 will be written into file3. 对于我当前的代码,如果缺少file2中的第二行,则file2中的所有后续行都将写入file3。 How can skip the missing line in this case and only find out exactly different lines? 在这种情况下如何跳过缺失的行只能找出完全不同的行? Any ideas for this? 有什么想法吗?

int file1LineNo = 0;
int file2LineNo = 0;
string file1lineStr;
string file2Str;
SortedDictionary<int, Object[]> info = new SortedDictionary<int, Object[]>();
string[] file1Lines = File.ReadAllLines(file1Name);
string[] file2Lines = File.ReadAllLines(file2Name);
while (file1LineNo<file1Lines.length)
{
  file1lineStr = file1Lines[file1LineNo];
  if (file1lineStr != null)
  {
    while(file2LineNo<file2Lines.Length)
    {
      file2Str = file2Lines[file2LineNo];
      if (file1LineNo == file2LineNo)
      {
         if(!file2Str.Trim().Equals(file1Str.Trim()))
         {
           Result = false;
           info.Add(rowNumber1++, new Object[]{"", file1lineStr, file2Str});
         }
      break;
      }
     file2LineNo++;
    }
   }
  }
 file1LineNo++;
}
foreach(var infoValue in info)
{
   Object[] objectArr = info.Value;
   for (int I=0; I<objectArr.Length; i++)
  {
    result.WriteResultToFile3(....);
   }
   rowed++;
}
 return Result;
}
}
}

This will output the difference in two files, outputting a diff file and a text file with the same content. 这将输出两个文件中的差异,输出diff文件和具有相同内容的文本文件。 You can further modify the output to your choosing using the git diff options. 您可以使用git diff选项进一步修改输出。 You will need the Git client installed on your machine or embed it in your source code using a NuGet package perhaps... 你需要在你的机器上安装Git客户端,或者使用NuGet包将它嵌入你的源代码中......

https://git-scm.com/downloads https://git-scm.com/downloads

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace PowerShell_Export_Differences
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        string directory = "C:/PowershellTest";

        using (PowerShell powershell = PowerShell.Create())
        {
            powershell.AddScript(String.Format(@"cd {0}", directory));
            powershell.AddScript(@"git init");
            powershell.AddScript(@"git diff --no-index  Text1.txt Text2.txt > Text3.diff");
            powershell.AddScript(@"git diff --no-index  Text1.txt Text2.txt > Text3.txt");
            Collection <PSObject> results = powershell.Invoke();
            Console.Read();
        }
    }
}
}

Sample output: 样本输出:

在此输入图像描述

Your question is a little inspecific and you really need to show some code. 你的问题有点疑问,你真的需要展示一些代码。

Saying that if its basically "blank / empty" lines you have an issue with then try the following. 如果它基本上是“空白/空”行你有问题然后尝试以下。

Remove all the "blank / empty" lines from both file 1 and file 2 then do the compare. 从文件1和文件2中删除所有“空白/空”行然后进行比较。

This to be honest its a bit "bodgy" but it may answer your question. 说实话,它有点“笨拙”,但它可能会回答你的问题。

Show what you have already tried (code wise) and folk may be more willing to write something. 展示你已经尝试过的东西(代码明智),民间可能更愿意写点东西。

you have to read each line and store the lines in different string, then just compare. 你必须读取每一行并将行存储在不同的字符串中,然后进行比较。 eg 例如

string s1 = "first"; string s1 =“first”; // store the line you have read from file1 //存储从file1读取的行

string s2 = "second"; string s2 =“second”; // store the line you have read from file2 //存储您从file2读取的行

if( !s1.Equals(s2) ) if(!s1.Equals(s2))

{ {

 // store the Result in file3 if there are not Equals.

} }

NOTE: this code is working when the order of the lines are the same in the both files. 注意:当两个文件中的行顺序相同时,此代码正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM