首页 > 解决方案 > 如何在现有 tcl 文件中的两行文本之间写入?

问题描述

我想在现有 tcl 文件的两行之间写入。例如,我想在第 41 行和第 42 行之间写一些文本。新文本应该在第 42 行,42 中的旧文本应该到 43 并重复直到最后一行向下移动 1。

我试过这个,https://stackoverflow.com/a/37806536,但文本被替换了。

当前的:

set bCheckIPs 1
if { $bCheckIPs == 1 } {
   set list_check_ips "\ 
ip:proc_sys_reset:5.0\
ip:processing_system7:5.5\
ip:xlconstant:1.1\
ip:axi_dma:7.1\
ip:axis_data_fifo:2.0\
hls:trace_cntrl_32:1.4\
"

我的预期输出:


set bCheckIPs 1
if { $bCheckIPs == 1 } {
   set list_check_ips "\ 
ip:proc_sys_reset:5.0\
ip:processing_system7:5.5\
ip:xlconstant:1.1\
ip:axi_dma:7.1\
ip:sample:1.0\
ip:axis_data_fifo:2.0\
hls:trace_cntrl_32:1.4\
"

我想在 ip:axi_dma:7.1\ 和 ip:axis_data_fifo:2.0\ 之间添加 ip:sample:1.0\

标签: filetclfile-handling

解决方案


anwser 就在附近,就在下一个

使用该代码,您可以获得这样的过程:

proc addtxtline {filename lineadd textadd} {
    # where filename: the file
    # lineadd: number of line to add - starting in zero
    # textadd: text to add
    set fp [open $filename]
    set lines [split [read $fp] "\n"]
    close $fp

    set lines [linsert $lines $lineadd $textadd]
    # Read a line with lindex, find a line with lsearch
    # Replace a line with lset, replace a range of lines with lreplace

    set fp [open $filename w]
    puts $fp [join $lines "\n"]
    close $fp 
}

猜测你的文件是“settings.txt”,你会以这种方式调用函数:

addtxtline settings.txt 7  "ip:sample:1.0\\"

萨卢多斯,


学分:Donal Fellows


推荐阅读