Find a specific string from line and match it to whole document

i have a text file which is about 1000 lines. as an example some of the first lines are given as bellow.

1
2
3
4
5
6
CallllStarted_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
CallStarted_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
CallStarted_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end
CallStartedAck_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
CallStartedAck_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
CallStartedAck_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end.



What i want to do: i would like to read the first line,after reading the line i would like to take under consideration all the characters after Cap900 then match these characters with the following whole text if match is found write that line under the line that its characters has been matched. After all lines matched i want to write these lines to other files which looks like this.

CallStarted_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
CallStartedAck_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
CallStarted_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
CallStartedAck_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
CallStarted_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end
CallStartedAck_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end.



So far i have done how to read the file and read all line like this.

ifstream inFile;
inFile.open(inFileName);//open the input file

stringstream strStream;
strStream << inFile.rdbuf();//read the file
string original;
getline(inFile, original, '\0');
cout << original << endl; // print all the line from file
cout << "\nEnd of file reached\n" << endl;
But i am stuck on what to do now, any help would be appreciated.
Last edited on
Pseudo-code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

Loop:
    Read a line

    Isolate the first group of characters (I assume the first space)

    Save this isolated group into storage for later processing.
End Loop

Close the input file

Reopen the input file (so you can start at the beginning)

Open a new file

Loop:
    Read a line

    Isolate the first group of characters as before

    Now, search your saved isolated groups for a match

    If found:
        Write the current line to the new file
        Write the matched chars to the new file

End Loop


You could save the isolated groups in an STL container.
So you want to sort it. The problem with just reading each line is that you have to keep up with which lines have been used and which haven't for 1000+ lines.

I think the easiest way is to use a vector.

http://www.cplusplus.com/reference/vector/vector/

So you would have 2 basic task, read each line into the vector, then sort
thanks guys i have developed the code according to your suggestions. but somehow its working weird. Not giving the result i am expecting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <vector>
#include <iostream>

using namespace std;



int main()
{

    string line,splited;
    int first, last;
    ifstream  stream1("test.txt");
    ofstream stream2("output.txt");

    //Fetch test.txt into an array: vector<string>my_array (It's faster)
    vector<string>my_array;
    while( std::getline( stream1, line ) )
    {
        my_array.insert(my_array.end(), line);
        //cout << line << endl;
    }

    //For each element:
    for(int i = 0 ; i< my_array.size() ; i++){

        //fetch current line:
        line = my_array[i];

        //Find substring that match with the pattern we want:
         first = line.find('_');
         last = line.find_last_of('.');
         splited = line.substr (first+1,last-first);

        //Find from next position all the lines that match with splited:
        int next = i+1;
        string next_line = line;
        for(next ; next < my_array.size() ; next++){

            if(next_line.find(splited) != string::npos){
                //Write into output.txt
                stream2 << next_line << endl;
            }
            next_line = my_array[next];
        }
        //Add a line:
        stream2 << " ----------------------------------------------------------------------- " << endl;


        //Continue looping...



    }
        //      - Get splitted string for current line

    stream1.close();
    stream2.close();
    return 0;

}


As input(lines in test.txt) you can go and see my first post.
And i want to output to be like this.

>>>>>>>>>>     CallStarted_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
<<<<<<<<<<     CallStartedAck_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
 ----------------------------------------------------------------------- 
>>>>>>>>>>     CallStarted_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
<<<<<<<<<<     CallStartedAck_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
 ----------------------------------------------------------------------- 
>>>>>>>>>>     CallStarted_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end
<<<<<<<<<<     CallStartedAck_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end
 
.

But i run the program that i have written above results as follow

CallllStarted_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
CallStartedAck_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
 ----------------------------------------------------------------------- 
CallStarted_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
CallStartedAck_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
 ----------------------------------------------------------------------- 
CallStarted_Cap900 10.160.167.34 32514 unid to 10.9.11.61 32580 RGorusme at 2015 04 21 16 33 55 035.waa_end
 ----------------------------------------------------------------------- 
CallStartedAck_Cap900 10.160.159.37 32514 unid to 10.9.11.66 32740 RABIA-TUJIMET-CERYAN at 2015 04 21 16 33 54 921.waa_end
 ----------------------------------------------------------------------- 
CallStartedAck_Cap900 10.9.11.61 32572 3051905 to 10.9.11.18 39994 79970056 at 2015 04 21 16 33 54 994.waa_end
 ----------------------------------------------------------------------- 
 ----------------------------------------------------------------------- 


what am i doing wrong?? waiting for your responses
You are not keeping track of which lines have already been processsed.

http://ideone.com/D6KQT6
Topic archived. No new replies allowed.