TCL - 在文件中查找规则模式并返回出现次数和出现次数
我正在编写一段代码来从文件中 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
}
我得到的输出:
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"
I am writing a code to grep a regular expression
pattern from a file, and output that regular expression and the number of times it has occured.
Here is the code: I am trying to find the pattern "grep" in my file hello.txt:
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
}
Output that I got:
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 while 循环中检查
eof
通常是一个错误 - 检查gets
的返回码:另一个想法:如果你只是计算模式匹配,假设你的文件不太大:
It's usually a mistake to check for
eof
in a while loop -- check the return code fromgets
instead:Another thought: if you're just counting pattern matches, assuming your file is not too large:
该错误消息是由命令
eof $file2
引起的。原因是$file2
不是一个文件句柄(或通道),而是包含文件hello.txt
本身的内容。您可以使用set file2 [read $file1]
读取此文件内容。如果你想这样做,我建议将
$file2
重命名为$filecontent
并循环遍历每个包含的行:The error message is caused by the command
eof $file2
. The reason is that$file2
is not a file handle (resp. channel) but contains the content of the filehello.txt
itself. You read this file content withset file2 [read $file1]
.If you want to do it like that I would suggest to rename
$file2
into something like$filecontent
and loop over every contained line:格伦是当场。这是另一个解决方案:Tcl 附带了 fileutil 包,其中有 grep 命令:
如果您关心性能,请使用 Glenn 的解决方案。
Glenn is spot on. Here is another solution: Tcl comes with the fileutil package, which has the grep command:
If you care about performance, go with Glenn's solution.