tcl - extracting particular values from output -


i have pagent router output

set pagent_ouput "interface:    ethernet2/3  packetfilter:   2500 123bps 456.123pps packetfilter:   2300 345bps 345.548pps  interface:    ethernet3/4  packetfilter:   2500 123bps 896.163pps packetfilter:   2300 345bps 675.748pps" 

ethernet interfaces varies....i want extract pps value each ethernet interface want { {456.123 345.548} {896.163 675.748}}

if pagent_output varies

set pagent_output "interface:    ethernet2/3  packetfilter:   2500 123bps 456.123pps packetfilter:   2300 345bps 345.548pps packetfilter:   2300 645bps 445.548pps packetfilter:   2300 745bps 545.548pps  interface:    ethernet3/4  packetfilter:   2500 123bps 656.123pps packetfilter:   2300 345bps 745.548pps packetfilter:   2300 345bps 845.548pps packetfilter:   2300 345bps 945.548pps  interface:    ethernet3/5  packetfilter:   2500 123bps 156.123pps packetfilter:   2300 345bps 255.548pps packetfilter:   2300 345bps 375.548pps packetfilter:   2300 345bps 395.548pps"  list { {456.123 345.548 445.548 545.548} {656.123 745.548 845.548 945.548}  {156.123 255.548 375.548 395.548}} 

first, want split text pieces on interface lines, , want extract data pieces. (it's easier split problem way, though there other ways it, because easier think larger problems in terms of connected smaller problems rather 1 big problem.) we're going use regular expressions extraction; follow along tcl's exact dialect of res, sure check relevant manual page.

to split data sections each interface, recommend using textutil::split::splitx command tcllib.

package require textutil::split  set interface_data [textutil::split::splitx $pagent_output {(?n)^interface:.*$}] 

then, want pps values out of data each interface; regexp -all -inline options best tool this:

set result {} foreach item [lrange $interface_data 1 end] {     lappend result [regexp -all -inline {\m[0-9.]+(?=pps)} $item] } 

now, result variable holds after.


if you've upgraded tcl 8.6, can bit shorter through use of lmap:

package require textutil::split  set result [lmap item [lrange [textutil::split::splitx $pagent_output {(?n)^interface:.*$}] 1 end] {     regexp -all -inline {\m[0-9.]+(?=pps)} $item }] 

it's still same basic idea though; textutil::split::splitx divide things (because doing hand bit of drag) , regexp -all -inline in loop extract info.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -