我有格式file.txt的数据
afsdfds/sfsd/sfdsf/VISA_CTS_ec0cbf000an2n06x5_G3B7I1/sfdfdvged/sdvdsfgv ec0cbf000an2n06x5
afsggggdfds/sfsd/sfdsf/VISA_CTS_ec0clb000an2n10x5_G3B7I1/sfdggggfdvged/sdvdsfgv ec0clb000an2n10x5
afsdfds/fdddo9045374857_234242/sfsd/sfdsf/VISA_CTS_ec0cilb000an2n16x5_G3B7I1/sfdfdvged/sdvdsfgv ec0cilb000an2n16x5
qweryt/visa_ec0ceb000an2n20x5_tyiuop/gfds_sfgds_/clock_VISA_CTS_ec0ceb000an2n20x5_G3B7I1/hjkhfjhfd/asdfasdf ec0ceb000an2n20x5
我在Tcl中使用
package require fileutil
set filename file.txt
set pattern {ec0cbf|ec0clb|cb|ceb}
puts "[llength [fileutil::grep $pattern $filename]] occurances"
$tcl> 7 occurances
我希望我的regexp只返回file.txt中最正确的值,而我的最终答案是3次出现(ec0cbf000an2n06x5 ec0ceb000an2n20x5 ec0ceb000an2n20x5))而不是7,因为我没有检测到“cilb”
此file.txt是动态数据
答案 0 :(得分:0)
这可以解决您的问题:
set a [open test.txt r]
set data [read $a]
set c [split $data "\n"]
set cnt 0
% foreach d $c {
if {[regexp {ec0cbf|ec0clb|cb|ceb} $d]} {
puts "[lindex $d end]"
incr cnt
}
}
puts $cnt