【问题标题】:How do I redirect the output on multiple lines in the Tcl to a file?如何将 Tcl 中多行的输出重定向到文件?
【发布时间】: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


【解决方案1】:

通过glenn jackman的评论问题解决了,原来是这样的:

# 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

# Create New File
set fp [open /tmp/outputFile.txt w]

# 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]]

    # Insert output on the New File
    puts $fp $out

}

# Close File New
close $fp

# View File New
exec aterm -e vi /tmp/outputFile.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2017-11-09
    • 1970-01-01
    相关资源
    最近更新 更多