file - 如何在现有 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\
解决方案
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\\"
萨卢多斯,
推荐阅读
- c++ - 将 lua_error 与 LuaJIT 一起使用会跳过堆栈对象析构函数吗?
- sql - Distinct 导致查询不再按预期工作。我该如何解决这个问题?
- javascript - RegEx 在指令中不起作用 - Angular 4
- javascript - 如何在php中修改json_encode函数文件?
- java - 按下登录按钮时强制关闭
- java - Android Studio IDE 中的 ArrayIndexOutOfBoundsException
- c# - 如何查找两个时间跨度之间的记录时间
- pandas - 我想在 pandas 中插入一个新列,然后是某种分组条件
- javascript - 如果列表没有使用 Jquery 的值,如何删除跨度标签
- javascript - 无法在 chrome 扩展中执行 php 文件