Read an F5 Configuration File in excel (retrieve values between brackets in different lines)

Viewed 201

I have a configuration file with many blocks like the following (with nested brackets).

ltm pool /Common/This_Is_The_Name {
    load-balancing-mode least-connections-member
    members {
        /Common/Member01 {
            address 1.2.3.4
        }
        /Common/Member02 {
            address 2.3.4.5
        }
    }
    monitor /Common/tcp
}

I have tried looking for characters like } or { but they appear more than once in the text and the macro fails. Also, not all the configurations are the same or in the same order.

What I did is too long to paste and is not working.

How can I print this in excel in a format, for example:

POOL NAME | LOAD-BALANCING MODE | MEMBER 01 NAME | MEMBER 01 IP | MEMBER 02 NAME | MEMBER 02 IP | MONITOR
This is the name | Least-connection-member | Member01 | 1.2.3.4 | Member02 | 2.3.4.5 | /common/tcp

I am reading these values from a txt file.

1 Answers

It is brute force method parsing of the file though there may be other easy way of parsing the file. You may try this and modify to your requirement. Array used are dimmed as (1 to 100) for quick test, but you may use Redim to make efficient use of it. I made Output format something different to my choice, may also modify it to your choice.

Obviously the code would work correctly only if every starting curly brace have corresponding closing curly brace and nested correctly.

Code:

Option Explicit
Sub ParseConfigFile()
Dim Fno As Integer, Xstr As String, Fname As String
Dim lvl As Integer, i As Integer, x As Integer, y As Integer, Rw As Integer
Dim Ws As Worksheet, Xchr As String
Set Ws = ThisWorkbook.ActiveSheet

Fno = FreeFile
Fname = "C:\Users\user\Desktop\ConfigSample.txt"
Open Fname For Input As #Fno
Xstr = Input(LOF(Fno), Fno)
Close #Fno

Xstr = Replace(Xstr, vbCrLf, "")
Xstr = Replace(Xstr, "/", "|")


Dim Snest(1 To 100, 1 To 100) As Variant, Enest(1 To 100, 1 To 100) As Variant
Dim path(1 To 100) As Variant

For x = 1 To 100
For y = 1 To 100
Snest(x, y) = 0
Enest(x, y) = 0
Next
Next

lvl = 1
path(lvl) = 1
Snest(path(lvl), 1) = 1
Rw = 1
    For x = 1 To Len(Xstr)
    Xchr = Mid(Xstr, x, 1)
        If Xchr = "{" Then
        'Debug.Print "{", x, lvl, path(lvl)
        Enest(path(lvl), lvl) = x - 1
            'rw = rw + 1
            'For i = 1 To lvl
            'Ws.Cells(rw, i).Value = Trim(Mid(Xstr, Snest(path(i), i), Enest(path(i), i) - Snest(path(i), i) + 1))
            'Next
        lvl = lvl + 1
        path(lvl) = path(lvl) + 1
        Snest(path(lvl), lvl) = x + 1
        End If

        If Xchr = "}" Then
        'Debug.Print x, lvl, path(lvl)
        Enest(path(lvl), lvl) = x - 1
            Rw = Rw + 1
            For i = 1 To lvl
            Ws.Cells(Rw, i).Value = Trim(Mid(Xstr, Snest(path(i), i), Enest(path(i), i) - Snest(path(i), i) + 1))
            Next
        lvl = lvl - 1
        path(lvl) = path(lvl) + 1
        Snest(path(lvl), lvl) = x + 1
        End If
Next
End Sub

The output from the sample file created by copying sample text 4 times is as follows

enter image description here

Further simplifying for getting pure tree structure on excel cells

Option Explicit
Sub ParseConfigSimple()
Dim Fno As Integer, Xstr As String, Fname As String
Dim lvl As Integer, i As Integer, x As Integer, y As Integer, Rw As Integer
Dim Ws As Worksheet, Xchr As String
Set Ws = ThisWorkbook.ActiveSheet

Fno = FreeFile
Fname = "C:\Users\user\Desktop\ConfigSample.txt"
Open Fname For Input As #Fno
Xstr = Input(LOF(Fno), Fno)
Close #Fno

Xstr = Replace(Xstr, vbCrLf, "")
Xstr = Replace(Xstr, "/", "|")


Dim Spos As Long, Epos As Long, Plen As Long
Dim path(1 To 100) As Variant


lvl = 1
Spos = 1
Rw = 1
    'Make heading
    For x = 1 To 50
    Ws.Cells(Rw, x).Value = "Level " & x
    Next

    'Parse each item in nested level
    For x = 1 To Len(Xstr)
    Xchr = Mid(Xstr, x, 1)

        If Xchr = "{" Or Xchr = "}" Then
        Epos = x - 1
        Plen = Epos - Spos + 1
        path(lvl) = Trim(Mid(Xstr, Spos, Plen))
            If Len(path(lvl)) > 1 Then
            Rw = Rw + 1
            Ws.Cells(Rw, lvl).Value = path(lvl)
            End If
        Spos = x + 1
        Epos = x - 1
            If Xchr = "{" Then
            lvl = lvl + 1
            Else
            lvl = lvl - 1
            End If
        End If
Next
End Sub

The output will be like

enter image description here

Related