The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
Please vote in the Forum Structure Poll. Polling will close at 2PM EST on January 21, 2025.

Text file sorting/comparing question

supabeastsupabeast Registered User regular
edited October 2010 in Help / Advice Forum
I'm having trouble with some text sorting. I have two text files, file1 and file2. Each file is lines made u of a word, a space, and another word. file2 contains a subset of file1, but in a different order. Is there a program that can sort the entries in file2 to match the order of file1? Or just delete every entry in file1 that does not also appear in file2? Or if I sort them and use comm to get a list of the matching entries is there a way I can delete every entry in file1 that is not one of those matching entries? Should I just write a python script to dump both files to lists and iterate each file1 entry through all file2 entries and write the matches to a new file?

supabeast on

Posts

  • meekermeeker Registered User regular
    edited October 2010
    Sounds to me like Excel is your best bet.

    meeker on
  • SevorakSevorak Registered User regular
    edited October 2010
    supabeast wrote: »
    Should I just write a python script to dump both files to lists and iterate each file1 entry through all file2 entries and write the matches to a new file?
    This is what I would do, except use sets instead of lists if you care about efficiency. That assumes that each word appears only once in each file though, and that you don't care about the order of the words in the new file.

    Sevorak on
    steam_sig.png 3DS: 0748-2282-4229
  • M0NSTERM0NSTER Registered User regular
    edited October 2010
    if you know C++, this would be a snap, using the fstream library

    heres a program i whiped up for you, u can just compile it, or u can hit me up for the exe
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    string word1[11]; //assuming that there is 10 words in file
    string word2[11];
    int i = 0;
    int n = 0;
    
    int main()
    {
    ifstream file1;
    ifstream file2;
    
    file1.open("file1.txt");
    file2.open("file2.txt");
    
    
    while(!file1.eof())
    {
    ++i;
    getline(file1, word1[i], '\n');
    }
    file1.close();
    
    while(!file2.eof())
    {
    ++n;
    getline(file2, word2[n], '\n');
    }
    file2.close();
    
    ofstream output;
    output.open("output.txt");
    for(int i = 0; i < 10; ++i)
    {
    if(word1[i] == word2[i])
    {
    output << word1[i] << "\n";
    }
    }
    output.close();
    cout << "Done!" << "\n";
    system("pause");
    return 0;
    }
    

    M0NSTER on
    I don't know how to count
  • The AnonymousThe Anonymous Uh, uh, uhhhhhh... Uh, uh.Registered User regular
    edited October 2010
    meeker wrote: »
    Sounds to me like Excel is your best bet.
    Excel is used for spreadsheeting. Sorting algorithms =/= spreadsheeting. And what if the files happen to be hundreds of MB large?

    The Anonymous on
  • Sir Headless VIISir Headless VII Registered User regular
    edited October 2010
    meeker wrote: »
    Sounds to me like Excel is your best bet.
    Excel is used for spreadsheeting. Sorting algorithms =/= spreadsheeting. And what if the files happen to be hundreds of MB large?

    You could still do it in excel. I wouldn't though and would use python or c or whatever language you are most comfortable with.

    Sir Headless VII on
    Steam - Backpack - Bnet: SirHeadless #1154
    7KEFduI.jpg
Sign In or Register to comment.