Thread: search text from file

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    search text from file

    Hello,
    Is there a way to search a vector and return all occurrences of a string. I have read a text file into a vector and want to search throught the vector. For example I am searching the vector for all occurrences with the word ssh. It may be ssh, or it may be ssh [email protected]. I am not sure if this requires a regexp or some other step to return all occurrences of ssh.
    Thanks for your time,
    Brad

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So is this a vector of char, containing the entire file as one big array?
    Or a vector of strings, each containing a single line from the file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    the vector is a vector of strings. I used this to populate it:
    Code:
    while (getline(infile, line))
      {
        file.push_back (line);
      }
    Thanks for the response.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could probably just use
    string::find()


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I tried using find but it only returns ssh. So if there were 10 occurences of of the word "ssh", they are not all just ssh. Some are ssh [email protected]. I am trying to return all strings that contain ssh, not just the ones that just have ssh. I thought maybe use an iterator and increment each pass, but it still will only look for ssh, no other variant of it.
    Thanks for the reply.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are after finding <nothing|separator>ssh<nothing|separator>, rather than "any string that contains ssh.

    Then you would need some more code. Some [but certainly not ALL possible] alternative solutions:
    1. some sort of regexp parser that you can feed with what is acceptable separators.
    2. write some code that loops through the string (using an iterator, if you wish), and when you find the right separator(s), check if the following string matches the searched for string and that the next thing is a valid separator.
    3. Use the location recieved from find to do a secondary check of the characters before/after the found string, to see if they are acceptable separators or not.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    So, you are after finding <nothing|separator>ssh<nothing|separator>, rather than "any string that contains ssh
    I guess I should have worded it better. The string will always start with ssh, whether it is just ssh or ssh [email protected]. So in crude regexp format:
    Code:
    ^ssh*
    thanks again

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Am I missing something? Wouldn't
    Code:
    if (1 == str.find("ssh", 1))
    {
        cout << "Found it" << endl;
    }
    work?

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    This is what I have so far.
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    
    
    using namespace std;
    
    int main(int argc, char** argv)
    {
    	vector<string> phrase;
    	ifstream iFile(".bash_history");
    	string temp;
        if (! iFile)
        {
            cout << "Error opening input file" << endl;
            return -1;
        }
        while (getline(iFile,temp)) {
        	phrase.push_back(temp);
        }
            
        for (int i = 0; i < phrase.size(); i++) {
        	if (1 == phrase.at(i).find("ssh", 1)){
        		cout << "Found it: " << phrase.at(i) << endl;
        		
            }
           
        }
        
    	return 0;
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you want to check if your find is the first thing in the string?

    Then check if the location returned by find is zero (that is, the first position in the string). If not, then find obviously located the string ssh elsewhere (or not at all, but for the purposes here, we don't care which it is, so treat both as the same).

    It gets a bit more tricky if the string could have spaces or other separators at the beginning - if that's the case, perhaps preprocessing the string and removing those would help a bit.

    Edit: I didn't notice the other posts as I started writing a reply, got sidetracked and only got back a minute ago. I think the rags_to_riches solution is correct, except you should use 0 as result and start position.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Hmmm. Where 0 was search all from the doc I read, I figured string must be using 1 for start position, but apparently not. This seems to find all string starting with ssh:
    Code:
    if (0 == str.find("ssh"))
    {
        cout << "Found it" << endl;
    }

  12. #12
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I got it to work doing this, not sure if this is the best way or not?
    Code:
    for (int i = 0; i < phrase.size(); i++) {
        	found = phrase.at(i).find("ssh",0);
            if (found != string::npos) {
            	results.assign(phrase.at(i), found, phrase.at(i).size());
        		cout << results << endl;
        	}
            
        }
    This prints out al the lines containing ssh in the first of the string. Is this way ok? Or would you suggest a better way? Also do you know why c++ does not have a regexp lib?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Search text in file
    By MarlonDean in forum C++ Programming
    Replies: 10
    Last Post: 05-16-2008, 03:51 AM
  3. search for text string in a file
    By basenews in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2007, 05:15 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM