【问题标题】:How to write between two lines of text in an existing tcl file?如何在现有 tcl 文件中的两行文本之间写入?
【发布时间】:2019-05-10 20:55:55
【问题描述】:

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

【问题讨论】:

    标签: file tcl file-handling


    【解决方案1】:

    anwser 就在附近,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\\"
    

    感谢,


    信用:Donal Fellows

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多