TCL:从文件中读取仅包含相关词的行
TCL: Read lines from file that contain only relevant words
我正在读取文件并对数据进行一些操作。
不幸的是,我收到以下错误消息:
无法分配 347392 字节
中止
由于文件很大,我只想读取包含一些单词的行(在“regexp_or”中描述)
有没有办法只读取包含“regexp_or”的行并保存 foreach 循环?
set regexp_or "^Err|warning|Fatal error"
set file [open [lindex $argv 1] r]
set data [ read $file ]
foreach line [ split $data "\n" ] {
if {[regexp [subst $regexp_or] $line]} {
puts $line
}
}
您可以通过 grep
:
获取您的输入
set file [open |[list grep -E $regexp_or [lindex $argv 1]] r]
但这取决于 grep
是否可用。要完全在 Tcl 中完成,您可以分块处理文件:
set file [open [lindex $argv 1] r]
while {![eof $file]} {
# Read a million characters
set data [read $file 1000000]
# Make sure to only work with complete lines
append data [gets $file]
foreach line [lsearch -inline -all -regexp [split $data \n] $regexp_or] {
puts $line
}
}
close $file
我正在读取文件并对数据进行一些操作。 不幸的是,我收到以下错误消息:
无法分配 347392 字节 中止
由于文件很大,我只想读取包含一些单词的行(在“regexp_or”中描述)
有没有办法只读取包含“regexp_or”的行并保存 foreach 循环?
set regexp_or "^Err|warning|Fatal error"
set file [open [lindex $argv 1] r]
set data [ read $file ]
foreach line [ split $data "\n" ] {
if {[regexp [subst $regexp_or] $line]} {
puts $line
}
}
您可以通过 grep
:
set file [open |[list grep -E $regexp_or [lindex $argv 1]] r]
但这取决于 grep
是否可用。要完全在 Tcl 中完成,您可以分块处理文件:
set file [open [lindex $argv 1] r]
while {![eof $file]} {
# Read a million characters
set data [read $file 1000000]
# Make sure to only work with complete lines
append data [gets $file]
foreach line [lsearch -inline -all -regexp [split $data \n] $regexp_or] {
puts $line
}
}
close $file