在文件中查找搜索字符串并打印该行中的下一个单词 [英] Looking for a search string in a file and printing next word in that line
本文介绍了在文件中查找搜索字符串并打印该行中的下一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,考虑一个文件abc.txt"具有以下内容
for example, consider a file "abc.txt" has following content
{apple_Text "1"}
{banana_Text "2"}
{orange_Text "3"}
现在,我想在该文件中搜索apple_Text"关键字,如果找到它应该在其中打印第二列值,即1".我可以知道如何在 Tcl 中做到这一点吗?
Now, I want to search "apple_Text" keyword in that file and if found it should print second column value in that, i.e. "1". Can I know how to do that in Tcl??
推荐答案
这是我的解决方案:
set f [open abc.txt]
while {[gets $f line] != -1} {
if {[regexp {apple_Text\s+"(.*)"} $line all value]} {
puts $value
}
}
close $f
基本上,我在每一行中搜索apple_Text"并从该行中提取值.
Basically, I search each line for "apple_Text" and extract the value from the line.
这篇关于在文件中查找搜索字符串并打印该行中的下一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文