[英]Compare specific column values of two text files - C#
I have a case where I need to compare two text files (using C#), an incoming text file, and an existing text file, checking for changes/differences. 我有一个案例,我需要比较两个文本文件(使用C#),传入的文本文件和现有的文本文件,检查更改/差异。 Both text files have the same layout, 6 columns of the same type of data and column headers.
两个文本文件具有相同的布局,6列相同类型的数据和列标题。 So this is basically a check to see if the contents of the incoming file has any new data compared to the existing.
所以这基本上是检查传入文件的内容是否与现有文件相比有任何新数据。
The File.ReadAllText
method essentially does what I want, the problem is though, I only want to compare the values in the first two columns of the text files, and disregard the other trailing columns. File.ReadAllText
方法基本上做我想要的,问题是,我只想比较文本文件的前两列中的值,而忽略其他尾随列。
The text file layout is like the following: 文本文件布局如下所示:
Item #|Total |C3|C4|C5|C6 项目编号| |总计 | C3 | C4 | C5 | C6
123|7 |0|0|0|0 123 | 7 | 0 | 0 | 0 | 0
So if either the Item # or Total column have a change in the first file compared to the second, the comparing of the files should stop there, and it should be considered a file change. 因此,如果Item#或Total列在第一个文件中与第二个文件相比发生了变化,那么文件的比较应该停在那里,并且应该将其视为文件更改。
What is a simple way to approach this? 什么是简单的方法来解决这个问题? Many thanks.
非常感谢。
This is an easy problem when you use string.Split
and LINQ's Take
and SequenceEqual
. 当您使用
string.Split
和LINQ的Take
和SequenceEqual
时,这是一个简单的问题。
bool AnyDifferent(string file1FullText, string file2FullText)
{
string[] file1Lines = file1FullText.Split('\n');
string[] file2Lines = file2FullText.Split('\n');
if (file1Lines.Length != file2Lines.Length)
return true;
for (int i = 0; i < file1Lines.Length; i++)
{
var file1LineSplit = file1Lines[i].Split('|');
var file2LineSplit = file2Lines[i].Split('|');
if (!file1LineSplit.Take(2).SequenceEqual(file2LineSplit.Take(2)))
return true;
}
return false;
}
Edit or using File.ReadLines
for improved performance (you don't have to read the whole files in before you start comparing them): 编辑或使用
File.ReadLines
以提高性能(在开始比较之前不必读取整个文件):
bool AnyDifferent(string file1Path, string file2Path)
{
using (var file1Enumerator = File.ReadLines(file1Path).GetEnumerator())
using (var file2Enumerator = File.ReadLines(file2Path).GetEnumerator())
{
while (true)
{
bool result1 = file1Enumerator.MoveNext();
bool result2 = file2Enumerator.MoveNext();
if (result1 != result2)
return true;
else if (!result1 && !result2)
return false;
var file1LineSplit = file1Enumerator.Current.Split('|');
var file2LineSplit = file2Enumerator.Current.Split('|');
if (!file1LineSplit.Take(2).SequenceEqual(file2LineSplit.Take(2)))
return true;
}
}
}
I would create a class for each Item : 我会为每个Item创建一个类:
class Item
{
public int ItemNumber { get; set; }
public int Total { get; set; }
}
then use a list and compare the data 然后使用列表并比较数据
List<Item> items = new List<Item>();
foreach (String line in File.ReadAllLines("filepath"))
{
Item item = new Item
{
ItemNumber = Convert.ToInt32(line.Split('|')[0]),
Total = Convert.ToInt32(line.Split('|')[1])
};
}
then you have a solid structure to compare them with 然后你有一个坚实的结构来比较它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.