查找{}(TCL)下可用的特定行

时间:2011-07-04 07:11:34

标签: tcl

我有一个包含大量数据的文件,如:

##########################################################
World is great
world has lots of counties
I am john who is staying in world
World {
abcd123 {
how are you 
}

abcd456 {
how is life
}

abcd789 {
what are you doing 
}

}
Hey going for moive
The life in this world is superb
#############################################

我希望获得“世界”下的信息 我希望输出为:

abcd123 {
how are you 
}

abcd456 {
how is life
}

abcd789 {
what are you doing 
}

1 个答案:

答案 0 :(得分:1)

set f [open filename r]
set contents [read -nonewline $f]
close $f

if {[info complete $contents]} {
  # safe to treat string as a list
  for {set i 0} {$i < [llength $s] - 1} {incr i} {
    if {[lindex $s $i] eq "World" && [llength [lindex $s $i+1]] > 1} {
      puts [lindex $s $i+1]
    }
  }
}
或者,或许这就像Donal的建议

set f [open $filename r]
set collecting false
set data ""
while {[gets $f line] != -1} {
  if {[regexp {^\s*World\s+\{\s*$} $line]} {
    set collecting true
  }
  if {$collecting} {
    append data $line \n
    if {[info complete $data]} break
  }
}
close $f
puts [lindex $data 1]