c# Compare two text files and generate a new one w

2020-03-30 03:22发布

I am looking for the best way to compare 2 text files (+-15000lines) quickly and get as output strings that are differents in the two files. 1st one is an old inventory, new one is the current inventory and I would like to generate an third one containing strings that are different between file2 & file1. (95% of the 2 files will be similar).

3条回答
The star\"
2楼-- · 2020-03-30 03:28

Very simple approach, assuming that similar means equal:

var file1Lines = File.ReadLines(file1Path);
var file2Lines = File.ReadLines(file2Path);
IEnumerable<String> inFirstNotInSecond = file1Lines.Except(file2Lines);
IEnumerable<String> inSecondNotInFirst = file2Lines.Except(file1Lines);

You can use foreach to enumerate the lines.

查看更多
虎瘦雄心在
3楼-- · 2020-03-30 03:41

If the output is in the same order, compare lines directly. You may need to skip line when the value is missing in the other file.

If, however, the output is not the same, then you might need to load the files into memory and look up the relevant inventory item from one file in to the other. Then do whatever you need when not-found or different.

查看更多
放荡不羁爱自由
4楼-- · 2020-03-30 03:48

You can use this diff library from Google. Look at the diff_main method that takes the 2 strings and returns a list of differences.

查看更多
登录 后发表回答