【发布时间】:2021-11-24 19:01:36
【问题描述】:
如何将 Tcl 中多行的“变量”输出重定向到文件。例如:
假设我的文件包含以下几行:
林哈 林哈 林哈 林哈等等……
更换:
线 线 线 线等等。
问题的原因是将输出重定向到新文件时,我只收到输出的最后一行。前几行在哪里???
这是我目前正在使用的程序:
# Open File
set open [open /tmp/linhas.txt]
# Read File
set read [read $open]
# Break File Line at the End (Space)
set line [split $read "\n"]
# Close File
close $open
# Going through a loop to capture only the necessary replacement
for {set i 1} {$i < 5} {incr i} {
set out [string map -nocase { {Linha} {Line} } [lindex $line $i]]
}
# Create New File
set fp [open /tmp/outputFile.txt w]
# Insert output on the New File
puts $fp $out
# Close File New
close $fp
# View File New
exec aterm -e vi /tmp/outputFile.txt
【问题讨论】:
-
out设置为单行,这就是原因。 -
@Shawn 我明白了,但我怎样才能得到其他行?我尝试使用“gets”创建一个新变量,但无济于事。
-
请参阅stackoverflow.com/a/70034562/9952196 以了解应该很容易适应您的需要的类似问题。
-
在循环之前打开输出文件。然后您可以将每个转换后的行放入文件中。
-
@glennjackman 好的,我明白了。在“for”循环之前打开并通过“puts $fp $out”将输出插入到“for”循环中,以解决相关问题。感谢您的贡献。
标签: tcl