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

发布于 2024-11-17 23:51:16 字数 581 浏览 2 评论 0原文

我正在编写一段代码来从文件中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

郁金香雨 2024-11-24 23:51:16

在 while 循环中检查 eof 通常是一个错误 - 检查 gets 的返回码:

set filename "hello.txt"
set pattern {grep}
set count 0

set fid [open $filename r]
while {[gets $fid line] != -1} {
    incr count [regexp -all -- $pattern $line]
}
close $fid

puts "$count occurrances of $pattern in $filename"

另一个想法:如果你只是计算模式匹配,假设你的文件不太大:

set fid [open $filename r]
set count [regexp -all -- $pattern [read $fid [file size $filename]]]
close $fid

It's usually a mistake to check for eof in a while loop -- check the return code from gets instead:

set filename "hello.txt"
set pattern {grep}
set count 0

set fid [open $filename r]
while {[gets $fid line] != -1} {
    incr count [regexp -all -- $pattern $line]
}
close $fid

puts "$count occurrances of $pattern in $filename"

Another thought: if you're just counting pattern matches, assuming your file is not too large:

set fid [open $filename r]
set count [regexp -all -- $pattern [read $fid [file size $filename]]]
close $fid
不知所踪 2024-11-24 23:51:16

该错误消息是由命令eof $file2 引起的。原因是 $file2 不是一个文件句柄(或通道),而是包含文件 hello.txt 本身的内容。您可以使用 set file2 [read $file1] 读取此文件内容。

如果你想这样做,我建议将 $file2 重命名为 $filecontent 并循环遍历每个包含的行:

foreach line [split $filecontent "\n"] {
  ... do something ...
}

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 file hello.txt itself. You read this file content with set 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:

foreach line [split $filecontent "\n"] {
  ... do something ...
}
腻橙味 2024-11-24 23:51:16

格伦是当场。这是另一个解决方案:Tcl 附带了 fileutil 包,其中有 grep 命令:

package require fileutil
set pattern {grep}
set filename hello.txt
puts "[llength [fileutil::grep $pattern $filename]] occurrences found"

如果您关心性能,请使用 Glenn 的解决方案。

Glenn is spot on. Here is another solution: Tcl comes with the fileutil package, which has the grep command:

package require fileutil
set pattern {grep}
set filename hello.txt
puts "[llength [fileutil::grep $pattern $filename]] occurrences found"

If you care about performance, go with Glenn's solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文