开发者

parse thru txt file with elements separated by {} brakets

Is there a way to parse thru a txt file that includes elements seperated by {}s

Here is a sample from the file:

virtual vs_devtnet_80 {
   snat automap
   pool pool_devtnet_80
   destination 167.69.107.41:http
   ip protocol tcp
   profiles {
      profile_http_health {}
      tcp-lan-optimized {}
   }
}
virtual vs_devdpp_4430 {
   snat automap
   pool pool_devdpp_5430
   destination 167.69.107.31:https
   ip protocol tcp
   persist devdpp
   profiles tcp-lan-optimized {开发者_StackOverflow社区}
}
virtual vs_devwww30_80 {
   snat automap
   pool pool_devwww30_80
   destination 167.69.107.46:http
   ip protocol tcp
   profiles {
      profile_http_health {}
      tcp-lan-optimized {}
   }
}

As you can see, the elements are separated, but with {}

Any help would be gladly appreciated. I was trying to use grep but it only returns one line...

I would like to be able to search by the top most element, for example searh.sh virtual vs_devtnet_80, and have it return the entire blob..furthermore perhaps be able to search for botht eh top layer and one of its sub layers, for example search.sh virtual vs_devtnet_80 pool which would return pool_devtnet_80


Something like:

cat .tmp | sed '/.*{/ ! { s/.*//g}'

this wont solve it completely, but i think it does something similar to what you want


Look at JSON pasers, they're written in all sort of languages, syntax looks similar enough to give you some ideas on how to tackle this.

Basically what you need to do is have a recursive function that calls itself whenever it encounters a '{' and returns the content whenever it encounters a '}'. I wrote an article on a lisp-like parser that actually does just that. Check it out here for inspiration: http://www.codeproject.com/KB/linq/TinyLisp.aspx

Rgds Gert-Jan


This is Tcl syntax, so you can set up a mechanism to run it as a Tcl script that creates a data structure of itself.

#!/usr/bin/env tclsh

# create a safe slave interpreter
set slave [interp create -safe]

# set up the slave to parse the input file
$slave eval {
    proc virtual {subkey body} {
        eval $body
    }
    proc unknown {cmd args} {
        upvar 1 subkey subkey  ;# access the 'subkey' variable from the 'virtual' proc
        if {[llength $args] == 1} {set args [lindex $args 0]}
        dict set ::data $subkey $cmd $args
    }
    set data [dict create]
}
$slave expose source

# now do the parsing in the slave
$slave eval source [lindex $argv 0]

# fetch the data structure
set data [$slave eval set data]

# and do stuff with it.
dict for {subkey subdict} $data {
    puts "$subkey destination = [dict get $subdict destination]"
}

And then, parser.tcl input_file outputs

vs_devaetnet_80 destination = 167.69.107.41:http
vs_devdpp_4430 destination = 167.69.107.31:https
vs_devwww30_80 destination = 167.69.107.46:http


This should give you the 2nd 'blob', for instance:

sed -n '/virtual vs_devdpp_4430.*/,/^}$/p' filename.txt

and pipe through grep -oP '(?<=pool ).*' to get what follows pool in that blob.


I ended up having to create a recursive function that first used strpos to find the search variable within the entire config file. Then using a recursive function pop'd on and pop'd off brackets to return the searched variable's entire body.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜