[英]Search a section of text, which occurs regularly in tcl
I have a file called log_file with following text: 我有一个名为log_file的文件,其中包含以下文本:
....some text....
line wire (1)
mode : 2pair , annex : a
coding : abcd
rate : 1024
status : up
....some text....
line wire (2)
mode : 4pair , annex : b
coding : xyz
rate : 1024
status : down
....some text....
The values may differ but the attributes are always the same. 值可能不同,但属性始终相同。 Is there a way to find each line wire and display their attributes?
有没有找到每条电线并显示其属性的方法? The number of line wires also may differ.
线的数量也可能不同。
EDIT: File doesn't have any blank lines. 编辑:文件没有任何空行。 There are more attributes but only these are needed.
有更多属性,但仅需要这些属性。 Can I get like the first "n" lines, instead of searching for every line?
我可以像前“ n”行一样,而不是搜索每行吗? ie if there is line wire (1), copy that line plus the next 4 lines.
例如,如果有线(1),则复制该线以及接下来的4条线。
And I am copying the searched lines to a output file $fout
, which I have used earlier in the script with the same $line
. 我将搜索到的行复制到输出文件
$fout
,该文件在前面的脚本中与$line
相同。
Given your sample: 根据您的样本:
set fh [open log_file r]
while {[gets $fh line] != -1} {
switch -glob -- $line {
{line wire*} {puts $line}
{mode : *} -
{coding : *} -
{rate : *} -
{status : *} {puts " $line"}
}
}
close $fh
outputs 输出
line wire (1)
mode : 2pair , annex : a
coding : abcd
rate : 1024
status : up
line wire (2)
mode : 4pair , annex : b
coding : xyz
rate : 1024
status : down
Edit: print the next "n" lines following the "line wire" line to a file 编辑:将“线路”行之后的下一个“ n”行打印到文件中
set in [open log_file r]
set out [open log_file_filtered w]
set n 4
while {[gets $in line] != -1} {
if {[string match {line wire*} $line]} {
puts $line
for {set i 1} {$i <= $n} {incr i} {
if {[gets $in line] != -1} {
puts $out " $line"
}
}
}
}
close $fh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.