Extracting specific data from text file

Pages: 12
Hi
i have a file name LTS_IMSBR.txt and it includes the following data:

User Physical Location
----------------------
Equipment ID = 172.22.5.74:2944
Termination ID = A124
Master/Slave type = Master

(Number of results = 1)

IMS user description
--------------------
Subscriber number = 3302552
Local DN set = 0
User type = H248 subscriber
Equipment ID = 172.22.5.74:2944
AGCU module number = 23
BGL user flag = False
Priority line flag = False
Public User Identity = 6793302552
Caller service profile index = 0
Called service profile index = 0
Register service profile index = 0
Route select source code = 0

--- END
>>-----------------------------------------------------------------------------------------------------

User Physical Location
----------------------
Equipment ID = 172.22.5.65:2944
Termination ID = A290
Master/Slave type = Slave

(Number of results = 1)

IMS user description
--------------------
Subscriber number = 3302553
Local DN set = 0
User type = H248 subscriber
Equipment ID = 172.22.5.65:2944
AGCU module number = 23
BGL user flag = False
Priority line flag = False
Public User Identity = 6793302553
Caller service profile index = 0
Called service profile index = 0
Register service profile index = 0
Route select source code = 0

--- END
>>-----------------------------------------------------------------------------------------------------


O&M #1833845
%%LST IMSBR:D=K'3312552,LP=0;%%
RETCODE = 0 Operation succeeded

User Physical Location
----------------------
Equipment ID = 172.22.5.73:2944
Termination ID = A174
Master/Slave type = Slave

(Number of results = 1)

IMS user description
--------------------
Subscriber number = 3312552
Local DN set = 0
User type = H248 subscriber
Equipment ID = 172.22.5.73:2944
AGCU module number = 23
BGL user flag = False
Priority line flag = False
Public User Identity = 6793312552
Caller service profile index = 0
Called service profile index = 0
Register service profile index = 0
Route select source code = 0

--- END
>>-----------------------------------------------------------------------------------------------------
s

I would like to extract the following data only with its corresponding values:
Subscriber number, Termination ID, Equipment ID, AGCU module number, Master/Slave type

Please if someone could provide me with a sample code.
Thank you
Last edited on
There's various ways of doing this. This might be the simplest:

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> titles {
        "Subscriber number",
        "Termination ID",
        "Equipment ID",
        "AGCU module number",
        "Master/Slave type"
    };

    std::ifstream fin("LTS_IMSBR.txt");
    std::string line;

    while (std::getline(fin, line)) {

        std::vector<std::string> values(titles.size());
        bool found = false;

        while (std::getline(fin, line)) {
            if (line.substr(0, 3) == "+++")
                break;

            for (size_t i = 0; i < titles.size(); ++i) {
                if (line.substr(0, titles[i].size()) == titles[i]) {
                    size_t pos = line.find('=');
                    if (pos < line.size()) {
                        pos = line.find_first_not_of(" ", pos + 1);
                        if (pos < line.size()) {
                            found = true;
                            values[i] = line.substr(pos);
                        }
                    }
                }
            }
        }

        if (found) {
            for (size_t i = 0; i < titles.size(); ++i)
                std::cout << titles[i] << ": " << values[i] << '\n';
            std::cout << '\n';
        }
    }
}

i ran your code, but different errors are appearing.
what should i do
Last edited on
edwards6 wrote:
what should i do


You should state what those errors are. I just compiled and ran @dutch's code and it ran perfectly.

Subscriber number: 3302552
Termination ID: A124
Equipment ID: 172.22.5.74:2944
AGCU module number: 23
Master/Slave type: Master

Subscriber number: 3302553
Termination ID: A290
Equipment ID: 172.22.5.65:2944
AGCU module number: 23
Master/Slave type: Slave

Subscriber number: 3312552
Termination ID: A174
Equipment ID: 172.22.5.73:2944
AGCU module number: 23
Master/Slave type: Slave
Last edited on
@lastchance how to i include snipped picture of the error?
You need to compile it as C++11. So with gcc, for example, you would add -std=c++11 to the compile line.
by the way im using "Dev C++" to run the code. Should i use a different software? Please advice
This might be the "latest" version of Dev C++: http://orwelldevcpp.blogspot.com/
@dutch I have installed the latest version of Dev C++ but i did not understand this step:
"You need to compile it as C++11. So with gcc, for example, you would add -std=c++11 to the compile line."
Last edited on
i followed the link and enabled -std=c++11. All errors are now eliminated but it doesn't give any output. Just a blank command prompt.
@lastchance which IDE did u use? it gave the desired output with any error.
@lastchance which IDE did u use? it gave the desired output with any error.

That's because he knows what he's doing.
You probably don't have the input file in the correct location.

I should've added code to check that the file was opened, like this:

1
2
3
4
5
    std::ifstream fin("LTS_IMSBR.txt");
    if (!fin) {
        std::cerr << "Cannot open input file.\n";
        return 1;
    }

Last edited on
this is the output im currently getting. the 1st 4 corresponding values is not showing.

Subscriber number:
Termination ID:
Equipment ID:
AGCU module number:
Master/Slave type: Master

Subscriber number:
Termination ID:
Equipment ID:
AGCU module number:
Master/Slave type: Slave

Subscriber number:
Termination ID:
Equipment ID:
AGCU module number:
Master/Slave type: Slave
The spacing of your input file was probably lost when you posted it.
You need to post it within "output" tags to maintain spacing so I can see what it really looks like.
Something like this:

[output]
post the text inside the output tags
[/output]
Here's a version that ignores differences in spacing:

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
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cctype>

// true if line begins with s, but ignoring spaces
bool is_match(const std::string& line, const std::string& s) {
    size_t i = 0, j = 0;
    for (;;) {
        while (i < line.size() && isspace(line[i]))
            ++i;
        if (i >= line.size())
            break;
        while (j < s.size() && isspace(s[j]))
            ++j;
        if (j >= s.size())
            break;
        if (line[i++] != s[j++])
            return false;
    }
    return j == s.size();
}

int main() {
    std::vector<std::string> titles {
        "Subscriber number",
        "Termination ID",
        "Equipment ID",
        "AGCU module number",
        "Master/Slave type"
    };

    std::ifstream fin("LTS_IMSBR.txt");
    if (!fin) {
        std::cerr << "Cannot open input file.\n";
        return 1;
    }

    std::string line;

    while (std::getline(fin, line)) {

        std::vector<std::string> values(titles.size());
        bool found = false;

        while (std::getline(fin, line)) {
            if (line.substr(0, 3) == "+++")
                break;

            for (size_t i = 0; i < titles.size(); ++i) {
                if (is_match(line, titles[i])) {
                    size_t pos = line.find('=');
                    if (pos < line.size()) {
                        pos = line.find_first_not_of(" ", pos + 1);
                        if (pos < line.size()) {
                            found = true;
                            values[i] = line.substr(pos);
                        }
                    }
                }
            }
        }

        if (found) {
            for (size_t i = 0; i < titles.size(); ++i)
                std::cout << titles[i] << ": " << values[i] << '\n';
            std::cout << '\n';
        }
    }
}

@dutch It know displays the output.Thank you so much. Really appreciated your assistance.
Last edited on
@dutch Just a follow up question. How do I display my output as shown in the given link???

https://imgur.com/F0B2QsD

Please reply
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <cctype>

// true if line begins with s, but ignoring spaces
bool is_match(const std::string& line, const std::string& s) {
    size_t i = 0, j = 0;
    for (;;) {
        while (i < line.size() && isspace(line[i]))
            ++i;
        if (i >= line.size())
            break;
        while (j < s.size() && isspace(s[j]))
            ++j;
        if (j >= s.size())
            break;
        if (line[i++] != s[j++])
            return false;
    }
    return j == s.size();
}

int main() {
    std::vector<std::string> titles {
        "Subscriber number",
        "Termination ID",
        "Equipment ID",
        "AGCU module number",
        "Master/Slave type"
    };

    std::ifstream fin("LTS_IMSBR.txt");
    if (!fin) {
        std::cerr << "Cannot open input file.\n";
        return 1;
    }

    std::string line;

    std::cout << "|Subscriber number"
                 "|Termination ID"
                 "|  Equipment ID  "
                 "|AGCU module number"
                 "|Master/Slave type|\n";

    while (std::getline(fin, line)) {

        std::vector<std::string> values(titles.size());
        bool found = false;

        while (std::getline(fin, line)) {
            if (line.substr(0, 3) == "+++")
                break;

            for (size_t i = 0; i < titles.size(); ++i) {
                if (is_match(line, titles[i])) {
                    size_t pos = line.find('=');
                    if (pos < line.size()) {
                        pos = line.find_first_not_of(" ", pos + 1);
                        if (pos < line.size()) {
                            found = true;
                            values[i] = line.substr(pos);
                        }
                    }
                }
            }
        }
        auto sw = std::setw;
        if (found) {
            std::cout << '|'    << sw(11) << values[0] << "      |";
            std::cout << "   "  << sw( 4) << values[1] << "       |";
            std::cout           << sw(16) << values[2] << "|";
            std::cout           << sw( 8) << values[3] << "          |";
            std::cout << "    " << std::left << sw( 6) << values[4]
                                << std::right << "       |\n";
        }
    }
}

@dutch Lastly, how do I save my output on a text file?

Please reply
Last edited on
Pages: 12