这是一个例子......
档案1:
1; 01812345; BB100
2; 018ACBA5; BB100
3; 01955555; BB100
10; 01901022; BB100
文件2 :(不同的mac地址,缺少3;)
1; 01866666; BB101
2; 01877777; BB101
10; 01988888; BB101
如何快速确定我缺少3;
?我无法比较整行,因为我只需要在第一个;
之前比较第一个整数值
我需要一些自动化解决方案,因为我使用300个地址的列表,而不是连续的。
在对我的另一个答案的评论中,你说,这项任务是你经常做的事情,你也表达了自动化的愿望。这是一个小型的C ++程序,应该可以工作:像prg file1 file2
一样使用它来查找file1中的行,其中包含file2中缺少的键。要从file1中删除行,请切换参数的顺序:prg file2 file1
。
#include <iostream>
#include <string>
#include <map>
#include <fstream>
using namespace std;
typedef map< string, string > tMap;
bool readFileIntoMap( string fn, tMap &m)
{
ifstream inFile( fn, std::ios::in);
if( !inFile.good() ){
std::cout << "Could not open " << fn << std::endl;
return false;
}
string key, aLine;
string::size_type pos;
while ( inFile ) {
getline( inFile, aLine ) ;
pos = aLine.find( ';' );
if( pos != string::npos ) {
key = aLine.substr(0, pos);
} else {
key = "-1";
}
m[ key ] = aLine; // map key to complete line
} // of while
return true;
}
// check for each key of first file: if the key is present in the
// second file, if not: report the line from the first file
void findMissingKeys( tMap &leftMap, tMap &rightMap)
{
string leftKey;
for( auto &leftElem : leftMap) {
leftKey = leftElem.first;
auto it = rightMap.find( leftKey );
if( it == rightMap.end() ) {
// report missing line in second file
cout << leftElem.second << endl;
}
}
}
int main( int argc, char* argv[] ) {
if ( argc != 3 ) {
cerr << "Please provide exactly two filenames as argument!" << endl;
cerr << "Program will dump lines with a key present in first and missing in second file." << endl;
return 1;
}
tMap m1, m2;
readFileIntoMap( argv[1], m1 );
readFileIntoMap( argv[2], m2 );
findMissingKeys(m1,m2);
}
另一种选择是将两个文件的行插入新文件中,然后对新文件进行排序。现在,您需要在第一列中找到具有唯一编号的行。但我会使用比较工具或比较插件,如上所述。这使得差异更容易发现。