Understanding How to Extract Hierarchical Strings

Asked 1 years ago, Updated 1 years ago, 79 views

We would like to process the following text file into CSV file format using shell script.
If it can be processed on Linux, I don't care about the method.
I can't think of a way to realize it, so I would appreciate it if you could give me some advice.

·Text file

ltm virtual/Common/vip-A {
    destination/Common/192.168.1.1:80
    ip-protocol tcp
    mask 255.255.255.255
    pool/Common/pool-A
    profiles {
        /Common/fastl4_default{}
    }
    source 0.0.0.0/0
    translate-address enabled
    translate-port enabled
}
ltm virtual/Common/vip-B {
    destination/Common/192.168.1.2:80
    ip-protocol tcp
    mask 255.255.255.255
    pool/Common/pool-B
    profiles {
        /Common/clientssl-www.hoge.jp {
            context clientside
        }
        /Common/tcp{}
    }
    source 0.0.0.0/0
    translate-address enabled
    translate-port enabled
}
ltm pool/Common/pool-A {
    members {
        /Common/192.168.2.1:80 {
            address 192.168.2.1
        }
    }
    monitor/Common/tcp 
}
ltm pool/Common/pool-B{
    members {
        /Common/192.168.2.2:80 {
            address 192.168.2.2
        }
    }
    monitor/Common/tcp 
}

·Examples of CSV output

vip-A, 192.168.1.1—80, tcp, 255.255.255.255, pool-A, fastl4_default, 0.0.0.0/0, enabled, enabled
vip-B, 192.168.1.2—80, tcp, 255.255.255.255, pool-B, www.hoge.jp, 0.0.0.0/0, enabled, enabled
pool-A, 192.168.1.1—80, 192.168.2.1, tcp
pool-B, 192.168.1.2—80, 192.168.2.2, tcp

It will be a supplement.
·We are proceeding with the purpose of managing the actual machine's configuration file in a list.
·In the form of a hierarchical structure, I would like to make one block in parentheses into a single line.
 I came up with the idea of extracting it with the awk command, but I'm worried about what to do with the new line.

linux shellscript sh awk

2022-09-30 14:47

3 Answers

}I think you should terminate only the line and replace the line feed code other than the terminating line with , .

sed's/^}$/}^D/'|tr'\n'^E'|tr'^D'\n'|tr'^E'', '|sed's/^, //'

^D and ^E are control characters.Any character that does not appear in the input is fine.
The results are as follows.I didn't delete unnecessary information.
"In the form of a hierarchical structure, a single block in parentheses is made into a single line."

ltm virtual/Common/vip-A {, destination/Common/192.168.1.1:80, ip-protocol tcp, mask 255.255.255.255, pool/Common/pool-A, profiles {,/Common/fastl4_default {},}, source 0.0.0.0/0, translate-address, address, port }
ltm virtual/Common/vip-B{, destination/Common/192.168.1.2:80, ip-protocol tcp, mask 255.255.255.255, pool/Common/pool-B, profiles {,/Common/clientssl-www.hoge.jp {, context clients, }, /Common/tcp{}, }, source 0.0.0.0/0, translate-address, address }
ltm pool/Common/pool-A {, members {, /Common/192.168.2.1:80 {, address 192.168.2.1,}, monitor/Common/tcp,}
ltm pool/Common/pool-B{, members{,/Common/192.168.2.2:80 {,address192.168.2.2,},},monitor/Common/tcp,}


2022-09-30 14:47

ReallyReallyReallyIf you don't mind a dirty one-liner, it will look like this...

$cat/tmp/bigip.conf | perl-nE'($w)=$_=~m!^.*\s(?:/.*/)?([^{}\s]+)!;if(/^}\s*$/){say join",", grep{length}@buf;@buf=()}elsif(/^\s*[a-z]+\s*\{\s*$/){next}else{push@buf,$w}'
vip-A, 192.168.1.1—80, tcp, 255.255.255.255, pool-A, fastl4_default, 0.0.0.0/0, enabled, enabled
vip-B, 192.168.1.2—80, tcp, 255.255.255.255, pool-B, clientssl-www.hoge.jp, clientside, tcp, 0.0.0.0/0, enabled, enabled
pool-A, 192.168.2.1—80, 192.168.2.1, tcp
pool-B, 192.168.2.2—80, 192.168.2.2, tcp


2022-09-30 14:47

Only policy...

Perhaps the best fit for this case is to use the perl6 grammar.

However, I have never written perl, so I don't think I can create a full answer.


2022-09-30 14:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.