Extraction of data from .txt file

Hi Guys

i have a file name ABC.txt and it includes the following data:

login as: rokob
Using keyboard-interactive authentication.
Password:
Access denied
Using keyboard-interactive authentication.
Password:
Last login: Wed Jan 29 09:35:04 2020 from 10.2.2.59

Cmd > type 'help' for help

Cmd > READ,SBS,DN=3300111
CMDREC: 2020-01-29 10:27:04 : (003-001-3299) : READ,SBS,DN=3300111
DN: 3300111
APSID: 1
SSID: 1
SUBCLS: PRA
LINECLS: ISDN
SPCH: N
URDI: N
ADA: N
CHNET: N
SCRLOC: N
SCRTOL: N
SCRCOSTL: N
SCROTHER: N
SCRINT: N
OGP: ORD
OCHG: Y
TCHG: Y
ERCALLCLS: 0
CLIP: A
CMDRES: 2020-01-29 10:27:04 : (003-001-3299) : Success: 0: OK

Cmd > READ,SBS,DN=3312556
CMDREC: 2020-01-29 10:27:15 : (003-001-3300) : READ,SBS,DN=3312556
DN: 3312556
APSID: 1
SSID: 1
SUBCLS: PRA
LINECLS: ISDN
SPCH: N
URDI: N
ADA: N
CHNET: N
SCRLOC: N
SCRTOL: N
SCRCOSTL: N
SCROTHER: N
SCRINT: N
OGP: ORD
OCHG: Y
TCHG: Y
ERCALLCLS: 0
CLIP: A
CMDRES: 2020-01-29 10:27:15 : (003-001-3300) : Success: 0: OK

Cmd > READ,SBS,DN=3100950
CMDREC: 2020-01-29 10:27:25 : (003-001-3301) : READ,SBS,DN=3100950
DN: 3100950
APSID: 1
SSID: 1
SUBCLS: IL
LINECLS: LAG
ADA: N
CHNET: N
SCRLOC: N
SCRTOL: N
SCRCOSTL: N
SCROTHER: N
SCRINT: N
OGP: ORD
OCHG: Y
TCHG: Y
ERCALLCLS: 0
CLIP: A
CNP: A
CALLBAR: A
BARCLS12: Y
BARCLS13: Y
BARCLS14: Y
CMDRES: 2020-01-29 10:27:25 : (003-001-3301) : Success: 0: OK

Cmd > READ,SBS,DN=3100959
CMDERR: < DN : 3100959 does not exists in database >
CMDRES: 2020-01-29 10:27:35 : () : Error: 0303027: DN DOES NOT REGISTER

Cmd > READ,SBS,DN=3100952
CMDREC: 2020-01-29 10:27:44 : (003-001-3302) : READ,SBS,DN=3100952
DN: 3100952
APSID: 1
SSID: 1
SUBCLS: IL
LINECLS: LAG
ADA: N
CHNET: N
SCRLOC: N
SCRTOL: N
SCRCOSTL: N
SCROTHER: N
SCRINT: N
OGP: ORD
OCHG: Y
TCHG: Y
ERCALLCLS: 0
CMDRES: 2020-01-29 10:27:44 : (003-001-3302) : Success: 0: OK

Cmd > READ,SBS,DN=6728009
CMDREC: 2020-01-29 10:49:02 : (003-002-6357) : READ,SBS,DN=6728009
DN: 6728009
APSID: 2
SSID: 2
SUBCLS: IL
LINECLS: LAG
ADA: N
CHNET: N
SCRLOC: N
SCRTOL: N
SCRCOSTL: N
SCROTHER: N
SCRINT: N
OGP: ORD
OCHG: Y
TCHG: Y
ERCALLCLS: 2
CFU: A
CFUACTDN: 9072397
WUP: D
CALLBAR: A
BARCLS12: Y
REVCHG: A
CMDRES: 2020-01-29 10:49:03 : (003-002-6357) : Success: 0: OK

Cmd >

I would like to extract the following data only with its corresponding values:
DN, APSID,SSID,SUBCLS,LINECLS

Please if someone could provide me with a sample code i will really appreciate it.
Thank you
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>

// With C++20 you can use std::string::starts_with().
bool starts_with ( const std::string& str, const std::string& pre )
{
    std::size_t i = 0;
    for ( ; i < str.size() && i < pre.size(); ++i )
        if ( str[i] != pre[i] )
            return false;
    return i == pre.size();
}

int main()
{
    std::ifstream fin( "input.txt" );
    std::string prefixes[]
    {
        "DN:", "APSID:", "SSID:", "SUBCLS:", "LINECLS:"
    };

    for ( std::string line; std::getline( fin, line ); )
    {
        for ( const auto& pre: prefixes )
            if ( starts_with( line, pre ) )
            {
                std::cout << pre << ' '
                          << line.substr( pre.size() + 1 ) << '\n';
                break;
            }
    }
}

@dutch Just a follow up question. How do I display my output as shown in the given link??

https://imgur.com/xMKPlSr
There's a few possibilities. Here's one. I'm not doing your formatting for you, though. :)

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

// With C++20 you can use std::string::starts_with().
bool starts_with ( const std::string& str, const std::string& pre )
{
    std::size_t i = 0;
    for ( ; i < str.size() && i < pre.size(); ++i )
        if ( str[i] != pre[i] )
            return false;
    return i == pre.size();
}

int main()
{
    std::map<std::string, std::string> m;
    std::ifstream fin( "input.txt" );
    std::string prefixes[]
    {
        "DN:", "APSID:", "SSID:", "SUBCLS:", "LINECLS:"
    };

    std::cout << "DN  APSID  SSID  SUBCLS  LINECLS\n";

    for ( std::string line; std::getline( fin, line ); )
    {
        for ( const auto& pre: prefixes )
            if ( starts_with( line, pre ) )
            {
                m[pre] = line.substr( pre.size() + 1 );
                if (pre == "LINECLS:")
                {
                    std::cout << m["DN:"]      << ' '
                              << m["APSID:"]   << ' '
                              << m["SSID:"]    << ' '
                              << m["SUBCLS:"]  << ' '
                              << m["LINECLS:"] << '\n';

                    m.clear();
                }
                break;
            }
    }
}

Last edited on
@dutch I want to differentiate the corresponding values of columns by the use of "|"
how do i display my output as shown below?


|DN |APSID|SSID|SUBCLS|LINECLS|
|3300111| 1 |1 |PRA |ISDN |
|3312556| 1 |1 |PRA |ISDN |
|3100950| 1 |1 |IL |LAG |
|3100952| 1 |1 |IL |LAG |
|6728009| 2 |2 |IL |LAG |


Last edited on
I've already shown you how to do that. All you need to do is format it.
As you can see below I have been able to format the heading, but how do i format the corresponding values?

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

// With C++20 you can use std::string::starts_with().
bool starts_with ( const std::string& str, const std::string& pre )
{
    std::size_t i = 0;
    for ( ; i < str.size() && i < pre.size(); ++i )
        if ( str[i] != pre[i] )
            return false;
    return i == pre.size();
}

int main()
{
    std::map<std::string, std::string> m;
    std::ifstream fin( "input.txt" );
    std::string prefixes[]
    {
        "DN:", "APSID:", "SSID:", "SUBCLS:", "LINECLS:"
    };

    std::cout << "|DN | APSID | SSID | SUBCLS | LINECLS|\n";

    for ( std::string line; std::getline( fin, line ); )
    {
        for ( const auto& pre: prefixes )
            if ( starts_with( line, pre ) )
            {
                m[pre] = line.substr( pre.size() + 1 );
                if (pre == "LINECLS:")
                {
                    std::cout << m["DN:"]      << ' '
                              << m["APSID:"]   << ' '
                              << m["SSID:"]    << ' '
                              << m["SUBCLS:"]  << ' '
                              << m["LINECLS:"] << '\n';

                    m.clear();
                }
                break;
            }
    }
}
Last edited on
You're looking at use of #include <iomanip>, setw, right, left etc

Use the tutorials in and around here including sample programs.

http://www.cplusplus.com/reference/iomanip/setw/
Topic archived. No new replies allowed.