Tcl 将两个匹配项之间的文件内容复制到其他文件

Tcl 将两个匹配项之间的文件内容复制到其他文件,tcl,Tcl,我有一个文件,其中的数据如下所示: Files { string1 string2 string3 ... ... } 但是我想将{&}之间的内容复制到其他文件,即string1,string2,这两个大括号与其他文件在不同的行中。由于Tcl非常灵活,并且该文件具有Tcl-ish语法,我会这样做: proc Files {data} {set ::files_data $data} source data_file set fin [open other_file r] set

我有一个文件,其中的数据如下所示:

Files {

string1

string2

string3
 ... 
 ...

}

但是我想将
{
&
}
之间的内容复制到其他文件,即
string1
string2
,这两个大括号与其他文件在不同的行中。

由于Tcl非常灵活,并且该文件具有Tcl-ish语法,我会这样做:

proc Files {data} {set ::files_data $data}
source data_file

set fin [open other_file r]
set fout [open other_file.tmp w]
set have_string1 false
while {[gets $fin line] != -1} {
    if {$have_string1 && [string match "string2" $line]} {
        puts $fout $files_data
    }
    puts $fout $line
    if {[string match "string1" $line]} {
        set have_string1 true
    }
}
close $fin
close $fout

# next line only if you need to keep a backup of the original
file link -hard    other_file.bak other_file 
file rename -force other_file.tmp other_file

到目前为止有什么尝试吗?您可以创建一个变量,告诉您是否在大括号
{}
内。如果看到
{
,将变量设置为1,只要它等于1,就复制该行。当看到
}
时,将其设置为0并停止复制,以此类推。