例如,考虑一个文件“abc.txt”具有以下内容
{apple_Text "1"}
{banana_Text "2"}
{orange_Text "3"}
Run Code Online (Sandbox Code Playgroud)
现在,我想在该文件中搜索“apple_Text”关键字,如果找到它应该在其中打印第二列值,即“1”。我可以知道如何在 Tcl 中做到这一点吗?
这是我的解决方案:
set f [open abc.txt]
while {[gets $f line] != -1} {
if {[regexp {apple_Text\s+"(.*)"} $line all value]} {
puts $value
}
}
close $f
Run Code Online (Sandbox Code Playgroud)
基本上,我在每一行中搜索“apple_Text”并从该行中提取值。