读写TCL上的特定行
亲爱的,我在“data.dat”文件中有以下信息(x y z): 我需要重新填写以下信息:读写TCL上的特定行,tcl,Tcl,亲爱的,我在“data.dat”文件中有以下信息(x y z): 我需要重新填写以下信息: {24.441 53.481 41.474} {23.920 53.389 42.572} {23.920 53.389 42.572} {24.470 52.228 42.012} {24.470 52.228 42.012} {24.875 51.313 42.524} {24.875 51.313 42.524} {23.663 51.323 42.701} 这适用于大型数据文件。我怎么能在TCL
{24.441 53.481 41.474} {23.920 53.389 42.572}
{23.920 53.389 42.572} {24.470 52.228 42.012}
{24.470 52.228 42.012} {24.875 51.313 42.524}
{24.875 51.313 42.524} {23.663 51.323 42.701}
这适用于大型数据文件。我怎么能在TCL做到这一点呢。提前谢谢你的帮助
set infile "data.dat"
set outfile [file tempfile]
set in [open $infile r]
set out [open $outfile w]
gets $in prev_line
while {[gets $in line] != -1} {
puts $out [format "{%s} {%s}" $prev_line $line]
set prev_line $line
}
close $in
close $out
# remove next line if you don't need to keep a backup of the initial file
file link -hard "${infile}.bak" $infile
# and overwrite the original file with the new contents
file rename -force $outfile $infile
或者,叫GNU awk去做
exec gawk -i inplace {
NR == 1 {prev = $0; next}
{printf "{%s} {%s}\n", prev, $0; prev = $0}
} data.dat
您可能想知道是否可以就地编辑数据。从技术上讲是的,但是如果数据改变了任何一行的长度(对于任何编程语言)都是危险的。正确写入单独的文件要容易得多。
exec gawk -i inplace {
NR == 1 {prev = $0; next}
{printf "{%s} {%s}\n", prev, $0; prev = $0}
} data.dat