English 中文(简体)
帮助从大文本档案中删除
原标题:Need Help Removing Entries from a Large Text File Based on the Contents of Another Text File

良好日子。 我可以真正利用你的帮助。 我有一份以以下格式分发的文件。

ID=1000000 
Name=Name1
Field1=Value1 
...(Fields 2 to 25)
Field26=Value26 

ID=1000001
Name=Name2
Field1=Value1 
...(Fields 2 to 25) 
Field26=Value26

ID=1000002
Name=Name2
Field1=Value1 
...(Fields 2 to 25) 
Field26=Value26 

...goes up to 15000

我有一份按行距分列的现役人员档案。

Name2
Name5
Name11
Name12 
...goes up to 1400 Random Names

如果在活跃的人的文本档案中找不到名字,我需要能够删除统计档案中的记录(ID、名称、领域1至26)。 在上述例子中,相关姓名记录(ID, 姓名,领域1至26)应予删除,因为它没有列入现役人员的文本档案。

I ve利用文本文本文本-->,尝试通过笔记本-++对统计档案进行重新设计;Find/Replace试图将其转换成一个 com的档案,每个记录都通过细线分离。 我已重新安排会议。

ID       Name    Field1  ...Fields2 to Fields 25... Field26
1000000  Name1   Value1  ...Value2 to Value 25...   Value26
1000001  Name2   Value1  ...Value2 to Value 25...   Value26
1000002  Name2   Value1  ...Value2 to Value 25...   Value26

I ve以Excel为开端,I ve在我方kl使用卷宗制作了两张表格(统计表和积极名称表)。 我不敢确定如何自动处理此事。 除了删除不活跃的记录外,我的另一个问题就是将其重新写成旧格式。

我在试图把这一点推到一个小时的末尾。 是否有一种胜诉的解决办法要求我使用两卷档案之间的发现、复制、过去和开关? 不幸的是,我不得不以这种格式保存统计档案。

请帮助。 谢谢。

最佳回答

Here s a C++ program that will process the files for you:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <locale>
#include <set>
#include <string>
#include <vector>

//trim functions taken:
//http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring/217605#217605
//with a slight change because of trouble with ambiguity
static int myIsSpace(int test)
{
    static std::locale loc;
    return std::isspace(test,loc);
}
static std::string &rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(myIsSpace))).base(), s.end());
    return s;
}

static std::string &ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(myIsSpace))));
    return s;
}

static std::string &trim(std::string &s) {return ltrim(rtrim(s));}

int main(int argc,char * argv[])
{
    std::ifstream peopleFile;
    peopleFile.open("people.txt");

    if (!peopleFile.is_open()) {
        std::cout << "Could not open people.txt" << std::endl;
        return -1;
    }

    std::set<std::string> people;

    while (!peopleFile.eof()) {
        std::string somePerson;
        std::getline(peopleFile,somePerson);
        trim(somePerson);
        if (!somePerson.empty()) {
            people.insert(somePerson);
        }
    }

    peopleFile.close();

    std::ifstream statsFile;
    statsFile.open("stats.txt");

    if (!statsFile.is_open()) {
        std::cout << "could not open stats.txt" << std::endl;
        return -2;
    }

    std::ofstream newStats;
    newStats.open("new_stats.txt");

    if (!newStats.is_open()) {
        std::cout << "could not open new_stats.txt" << std::endl;
        statsFile.close();
        return -3;
    }

    size_t totalRecords=0;
    size_t includedRecords=0;

    bool firstRecord=true;
    bool included=false;
    std::vector<std::string> record;
    while (!statsFile.eof()) {
        std::string recordLine;
        getline(statsFile,recordLine);
        std::string trimmedRecordLine(recordLine);
        trim(trimmedRecordLine);

        if (trimmedRecordLine.empty()) {
            if (!record.empty()) {
                ++totalRecords;

                if (included) {
                    ++includedRecords;

                    if (firstRecord) {
                        firstRecord=false;
                    } else {
                        newStats << std::endl;
                    }

                    for (std::vector<std::string>::iterator i=record.begin();i!=record.end();++i) {
                        newStats << *i << std::endl;
                    }
                    included=false;
                }

                record.clear();
            }
        } else {
            record.push_back(recordLine);
            if (!included) {
                if (0==trimmedRecordLine.compare(0,4,"Name")) {
                    trimmedRecordLine=trimmedRecordLine.substr(4);
                    ltrim(trimmedRecordLine);
                    if (!trimmedRecordLine.empty() &&  = ==trimmedRecordLine[0]) {
                        trimmedRecordLine=trimmedRecordLine.substr(1);
                        ltrim(trimmedRecordLine);
                        included=people.end()!=people.find(trimmedRecordLine);
                    }
                }
            }
        }
    }

    if (!record.empty()) {
        ++totalRecords;

        if (included) {
            ++includedRecords;

            if (firstRecord) {
                firstRecord=false;
            } else {
                newStats << std::endl;
            }

            for (std::vector<std::string>::iterator i=record.begin();i!=record.end();++i) {
                newStats << *i << std::endl;
            }
            included=false;
        }

        record.clear();
    }

    statsFile.close();
    newStats.close();

    std::cout << "Wrote new_stats.txt with " << includedRecords << " of the " << totalRecords << ((1==totalRecords)?" record":" records") << "found in stats.txt after filtering against the " << people.size() << ((1==people.size())?" person":" people") << " found in people.txt" << std::endl;

    return 0;
}
问题回答

暂无回答




相关问题
Which non-bold UTF-8 iPhone SDK font can I use here?

Problem: I create a UILabel with this font: label.font = [UIFont systemFontOfSize:15.0f]; Now this system font is bold, but I want to display pretty small text, and the text may also contain very ...

Limiting file upload type

Simple question. Is there a way to only allow txt files upon uploading? I ve looked around and all I find is text/php, which allows PHP. $uploaded_type=="text/php

Extract everything from PDF [closed]

Looking for solution to extract content from a PDF file (using console tool or a library). It will be used on server to produce on-line e-books from uploaded PDF files. Need to extract following ...

Flash & external form fields

Does anybody know if this is possible? I am trying to create a flash movie that will show / preview what I am typing into a field in a normal HTML form. The call to update the flash movie would most ...

Avoiding escape sequence processing in ActionScript?

I need to mimic C# functionality of the @ symbol when it precedes a string. @"C:AFilePath" for example What is the best way to do this? Also there are some sites that will escape larger ...

iPhone: Draw rotated text?

I want to draw some text in a view, rotated 90°. I m pretty new to iPhone development, and poking around the web reveals a number of different solutions. I ve tried a few and usually end up with my ...

Numbering in jQuery

How could I change the text below so that the text within it has a number appended to it. <div class="right">This is some text</div> <div class="right">This is some text</div>...

热门标签