如何在现有 tcl 文件中的两行文本之间写入?
How to write between two lines of text in an existing tcl file?
我想在现有的 tcl 文件中的两行之间写入。例如,我想在第 41 行和第 42 行之间写一些文本。新文本应该在第 42 行,而第 42 行的旧文本应该转到第 43 行,并重复直到最后一行向下移动 1。
我试过了,,但是文本被替换了。
当前:
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\
答案就在附近,。
使用该代码,您可以获得这样的过程:
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\"
祝你好运,
我想在现有的 tcl 文件中的两行之间写入。例如,我想在第 41 行和第 42 行之间写一些文本。新文本应该在第 42 行,而第 42 行的旧文本应该转到第 43 行,并重复直到最后一行向下移动 1。
我试过了,
当前:
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\答案就在附近,
使用该代码,您可以获得这样的过程:
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\"
祝你好运,