小编pra*_*thi的帖子

TCL - 在文件中查找常规模式并返回出现次数和出现次数

我正在编写一个代码来regular expression从文件中grep一个模式,并输出该正则表达式及其发生的次数.

这是代码:我试图在我的文件hello.txt中找到模式"grep":

set file1 [open "hello.txt" r]
set file2 [read $file1]
regexp {grep} $file2 matched
puts $matched
while {[eof $file2] != 1} {
set number 0
if {[regexp {grep} $file2 matched] >= 0} {
 incr number
}

puts $number
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出:

grep

--------
can not find channel named "qwerty
iiiiiii
wxseddtt
lsakdfhaiowehf'
jbsdcfiweg
kajsbndimm s
grep
afnQWFH
 ACV;SKDJNCV;
    qw  qde 
 kI UQWG
grep
grep"
    while executing
"eof $file2"
Run Code Online (Sandbox Code Playgroud)

tcl

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

tcl ×1