本文介绍了比较两个文本文件的第一列,并给出输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本文件,file1的内容是: -

400 ^ 20140211 ^ 0 ^ H ^ 010000 ^ A

400 ^ 20140211 ^ 0 ^ H ^ 020000 ^ A

400 ^ 20140211 ^ 0 ^ H ^ 030000 ^ A

400 ^ 20140211 ^ 0 ^ H ^ 040000 ^ A

400 ^ 20140211 ^ 0 ^ H ^ 050000 ^ A



文件2的内容是: -



400 ^ 20140211 ^ 0 ^ H ^ 010000 ^ A

400 ^ 20140211 ^ 0 ^ H ^ 020000 ^ C

400 ^ 20140211 ^ 0 ^ H ^ 030000 ^ B

400 ^ 20140211 ^ 0 ^ H ^ 040000 ^ A

402 ^ 20140211 ^ 0 ^ H ^ 050000 ^ A



我需要比较文本文件的第一列并检索第二个文件中存在且第一个文件中不存在的数据。其余的列我不想做任何事情。只有第一列。例如,这里402存在于file2中。所以输出将是402.就像那样。请帮助

I have two text files and the content of the file1 is :-
400^20140211^0^H^010000^A
400^20140211^0^H^020000^A
400^20140211^0^H^030000^A
400^20140211^0^H^040000^A
400^20140211^0^H^050000^A

content of the file 2 is :-

400^20140211^0^H^010000^A
400^20140211^0^H^020000^C
400^20140211^0^H^030000^B
400^20140211^0^H^040000^A
402^20140211^0^H^050000^A

I need to compare the first columns of the text files and retrieve the data which is present in the second file and not present in the 1st file. Rest of the columns I do not want to do anything . Only the first column . For example here 402 is present in file2 . So the output will be 402. Like that. Please help

推荐答案

StreamReader readFile_1 = new StreamReader(pathToFile_1);
StreamReader readFile_2 = new StreamReader(pathToFile_2);
//Get line from file and split into arrays
string inputString = readFile_1.ReadLine();     // File 1
string[] dataFile_1 = inputString.Split('^');

inputString = readFile_2.ReadLine();
string[] dataFile_2 = inputString.Split('^'); // File 2

// Extract the values
string valueFile_1 = dataFile_1[0];
string valueFile_2 = dataFile_2[0]; ;

// Test your values use whatever conditional
// values you wish such as the 402 value
if (valueFile_1 == valueFile_2)
{
    // Do whatever you do when they match
}
else
{
    // Do whatever you do when they don't match
}



祝你好运。


Good luck.


这篇关于比较两个文本文件的第一列,并给出输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 20:08