Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用tcl在n行之后插入文件中的代码行_Tcl - Fatal编程技术网

使用tcl在n行之后插入文件中的代码行

使用tcl在n行之后插入文件中的代码行,tcl,Tcl,我正在尝试编写一个tcl脚本,在找到正则表达式后需要插入一些代码行。 例如,在找到当前文件中最后一次出现的#define之后,我需要插入更多的#define代码行 谢谢 这不完全是您问题的答案,但这是一种适合shell脚本编写的任务类型(即使我的解决方案有点难看) 应该有用 编辑文本文件时,您可以在内存中读取并对其进行操作。由于您要处理该文本文件中的代码行,因此我们希望将该文件的内容表示为字符串列表(每个字符串都是一行的内容)。然后,我们可以使用lsearch(使用-regexp选项)查找插入位

我正在尝试编写一个tcl脚本,在找到正则表达式后需要插入一些代码行。 例如,在找到当前文件中最后一次出现的#define之后,我需要插入更多的#define代码行


谢谢

这不完全是您问题的答案,但这是一种适合shell脚本编写的任务类型(即使我的解决方案有点难看)


应该有用

编辑文本文件时,您可以在内存中读取并对其进行操作。由于您要处理该文本文件中的代码行,因此我们希望将该文件的内容表示为字符串列表(每个字符串都是一行的内容)。然后,我们可以使用
lsearch
(使用
-regexp
选项)查找插入位置(我们将在反向列表中执行此操作,以便查找最后一个位置而不是第一个位置),并且可以使用
linsert
进行插入

总的来说,我们得到的代码有点像这样:

# Read lines of file (name in “filename” variable) into variable “lines”
set f [open $filename "r"]
set lines [split [read $f] "\n"]
close $f

# Find the insertion index in the reversed list
set idx [lsearch -regexp [lreverse $lines] "^#define "]
if {$idx < 0} {
    error "did not find insertion point in $filename"
}

# Insert the lines (I'm assuming they're listed in the variable “linesToInsert”)
set lines [linsert $lines end-$idx {*}$linesToInsert]

# Write the lines back to the file
set f [open $filename "w"]
puts $f [join $lines "\n"]
close $f

搜索所有索引(并在最后一个索引中添加一个)是合理的,但插入的扭曲是相当丑陋的。(8.4之前的版本?升级版。)

这个问题确实需要一个Tcl脚本,它不是……您好,谢谢您的回答,但是这个方法不起作用。你能建议我如何在得到行号后插入一些代码,然后我需要插入代码。假设最后一个#define出现在第100DN行,我如何仅使用TCL在101处插入代码。如果您将其定制为从Tcl运行(通过它的
exec
命令),您的响应可能是正常的,但您没有。好的,感谢您从司法管辖权的角度正确理解它。。。我仍然很高兴能给出一个比Tcl答案简单得多的替代方案,而且你也不用写结果了。您也不会处理错误情况(没有文件,没有匹配)。Bash脚本编写很容易,直到您必须处理令人讨厌的边缘情况,然后困难就大了。另外,您还没有实际使用任何bash特性;任何unixshell都会这样做。我有点不清楚你在问什么。您是在寻找一种编写这样一个脚本的策略,还是有某种特定的Tcl构造导致了问题?您在第一句中提到“查找正则表达式”,在第二句中提到“查找最后一个匹配项”。这些是完全不同的。当使用
split[read$f]\n
读取文件内容时,您应该使用read的
-nonewline
选项,以避免列表中出现多余的尾随空白元素。@glenn或使用
put-nonewline$f[join$line\n]
# Read lines of file (name in “filename” variable) into variable “lines”
set f [open $filename "r"]
set lines [split [read $f] "\n"]
close $f

# Find the insertion index in the reversed list
set idx [lsearch -regexp [lreverse $lines] "^#define "]
if {$idx < 0} {
    error "did not find insertion point in $filename"
}

# Insert the lines (I'm assuming they're listed in the variable “linesToInsert”)
set lines [linsert $lines end-$idx {*}$linesToInsert]

# Write the lines back to the file
set f [open $filename "w"]
puts $f [join $lines "\n"]
close $f
# Read lines of file (name in “filename” variable) into variable “lines”
set f [open $filename "r"]
set lines [split [read $f] "\n"]
close $f

# Find the insertion index in the reversed list
set indices [lsearch -all -regexp $lines "^#define "]
if {![llength $indices]} {
    error "did not find insertion point in $filename"
}
set idx [expr {[lindex $indices end] + 1}]

# Insert the lines (I'm assuming they're listed in the variable “linesToInsert”)
set lines [eval [linsert $linesToInsert 0 linsert $lines $idx]]
### ALTERNATIVE
# set lines [eval [list linsert $lines $idx] $linesToInsert]

# Write the lines back to the file
set f [open $filename "w"]
puts $f [join $lines "\n"]
close $f
set filename content.txt
set fh [open $filename r]
set lines [read $fh]
close $fh


set line_con [split $lines "\n"]
set line_num {} 
set i 0
foreach line $line_con {
    if [regexp {^#define} $line] {
        lappend line_num $i
        incr i
    }
}

if {[llength $line_num ] > 0 } {
    linsert $line_con [lindex $line_num end] $line_insert
} else {
    puts "no insert point"
}


set filename content_new.txt
set fh [open $filename w]
puts $fh file_con
close $fh