从文件中挑出某些行
Pick out certain lines from files
我正在编写一个 tcl 脚本,该脚本从一个文件中读取并显示该文件中的某些行。我可以从文件 n 问题中读取,但我无法弄清楚如何在过滤掉我不需要的内容之前挑选出某些行。我正在将文件读取到列表中,然后使用以下代码将其过滤掉;
proc listFromFile {/.../.../.../file_test.file} {
set f [open /.../.../.../file_test.file r]
set data [split [string trim [read $f]]]
close $f
return $data
}
set f [listFromFile /.../.../.../file_test.file]
set f [lsort -unique $f]
set f [lsearch -all -inline $f "test_*"
文件中的行看起来像
$(eval $(call CreateUvmTest, other, test_runtest1 ...
$(eval $(call CreateUvmTest, KEYWORD, test_runtest2 ...
$(eval $(call CreateUvmTest, KEYWORD, test_runtest3 ...
$(eval $(call CreateUvmTest, other, test_runtest4 ...
在过滤掉不需要的所有其他内容之前,我如何从整体上挑选出包含 KEYWORD 的行?包含 KEYWORD 的行与其他测试一起随机出现在文件中。这有可能吗?
应该不难。首先,您可能希望在换行符处拆分文件的内容:
set data [split [string trim [read $f]] \n]
完成后,您可以使用多种技巧来select您想要的行,例如
lsearch -all -inline $data *KEYWORD*
我会强烈考虑
set lines [split [exec grep -Fw KEYWORD file_test.file] \n]
否则
proc select_lines {filename keyword} {
set fid [open $filename r]
while {[gets $fid line] != -1} {
if {[string match "*$keyword*" $line]} {lappend lines $line}
}
close $fid
return $lines
}
set lines [select_lines file_test.file KEYWORD]
我正在编写一个 tcl 脚本,该脚本从一个文件中读取并显示该文件中的某些行。我可以从文件 n 问题中读取,但我无法弄清楚如何在过滤掉我不需要的内容之前挑选出某些行。我正在将文件读取到列表中,然后使用以下代码将其过滤掉;
proc listFromFile {/.../.../.../file_test.file} {
set f [open /.../.../.../file_test.file r]
set data [split [string trim [read $f]]]
close $f
return $data
}
set f [listFromFile /.../.../.../file_test.file]
set f [lsort -unique $f]
set f [lsearch -all -inline $f "test_*"
文件中的行看起来像
$(eval $(call CreateUvmTest, other, test_runtest1 ...
$(eval $(call CreateUvmTest, KEYWORD, test_runtest2 ...
$(eval $(call CreateUvmTest, KEYWORD, test_runtest3 ...
$(eval $(call CreateUvmTest, other, test_runtest4 ...
在过滤掉不需要的所有其他内容之前,我如何从整体上挑选出包含 KEYWORD 的行?包含 KEYWORD 的行与其他测试一起随机出现在文件中。这有可能吗?
应该不难。首先,您可能希望在换行符处拆分文件的内容:
set data [split [string trim [read $f]] \n]
完成后,您可以使用多种技巧来select您想要的行,例如
lsearch -all -inline $data *KEYWORD*
我会强烈考虑
set lines [split [exec grep -Fw KEYWORD file_test.file] \n]
否则
proc select_lines {filename keyword} {
set fid [open $filename r]
while {[gets $fid line] != -1} {
if {[string match "*$keyword*" $line]} {lappend lines $line}
}
close $fid
return $lines
}
set lines [select_lines file_test.file KEYWORD]