tk - How to get selective data from a file in TCL? -


i trying parse selective data file based on key words using tcl,for example have file

... ... .. ...  data_start 30 abc1 xyz  90 abc2 xyz  214 abc3 xyz data_end ... ... ... 

how catch 30, 90 , 214 between "data_start" , "data_end"? have far(tcl newbie),

proc get_data_value{ data_file } {  set lindex 0 set fp [open $data_file r] set filecontent [read $fp]    while {[gets $filecontent line] >= 0} {  if { [string match "data_start" ]} {      #capture first number?      #use regex? or else?           if { [string match "data_end" ] } {              break         } else {              ##do nothing?         }     }  } close $fp } 

if file smaller in size, can use read command slurp whole data variable , apply regexp extract required information.

input.txt

data_start 30 abc1 xyz  90 abc2 xyz  214 abc3 xyz data_end data_start 130 abc1 xyz  190 abc2 xyz  1214 abc3 xyz data_end 

extractnumbers.tcl

set fp [open input.txt r] set data [read $fp] close $fp set result [regexp -inline -all {data_start.*?\n(\d+).*?\n(\d+).*?\n(\d+).*?data_end} $data] foreach {whole_match number1 number2 number3} $result {     puts "$number1, $number2, $number3" } 

output :

30, 90, 214 130, 190, 1214 

update :

reading larger file's content single variable cause program crash depends on memory of pc. when tried read file of size 890mb read command in win7 8gb ram laptop, got unable realloc 531631112 bytes error message , tclsh crashed. after bench-marking found able read file size of 500,015,901 bytes. program consume 500mb of memory since has hold data.

also, having variable hold data not efficient when comes extracting information via regexp. so, in such cases, better go ahead read content line line.

read more here.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -