如何在现有tcl文件的两行文本之间书写?

时间:2019-05-10 20:55:55

标签: file tcl file-handling

我想在现有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 \

1 个答案:

答案 0 :(得分:1)

答案接近just the next

使用该代码,您将获得如下所示的proc:

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\\"

Saludos,


信用:Donal Fellows