日常工作中,常常从各种rpt里搜寻自己需要的信息再去debug或是做优化,除了rpt,还有很多其他的文件需要我们抓取以及利用其中的信息。但是有时候我们要看的文件内容繁杂不直观,用眼睛去看不仅耗力还很难发现趋势。
菜鸟认为文件处理是很重要的一个技能,能帮助我们节省时间去分析问题本身,而文件处理涉及到一些command和操作方面的技巧,一旦掌握并且熟练应用,就能够使各种信息以你想要的方式呈现,清晰直观不说,还极大地提升了效率。 我对文件的处理主要集中于自己工作中遇到的方面,希望能给和我一样的小白一点帮助~
TCL可以对文件进行简单处理(虽然简单但也够我日常使用了)。先来点简单的基础命令:
open name ?access? ;#以某种方式打开文件
open file1 r ;#以只读方式打开文件 file1
open file2 w ;#以只写方式打开文件 file2
打开文件就意味着我们可以对其进行操作了,那怎么操作呢?什么都没有一个栗子直观->
set fid1 [ open file1 r];# 打开名为file1的文件作为只读文件,并将其设为fid1
set fid2 [ open file2 w];# 打开名为file2的文件作为只读文件,并将其设为fid2
gets $fid1 tmp ;# 获得文件的第一行(gets是逐行读取,再次gets就是获得第二行),并将该行内容存入tmp
puts $fid2 $tmp ;# 将变量tmp的内容写进$fid2中
close $fid1; close $fid2 ;# 关掉两个文件
file1中原本的内容如下
file2原本不存在,在上述操作之后,file2中内容如下:
因为我们只对file1中的第一行进行了gets操作,所有后面的内容并没有被存进file2。如果我们想把file1中的内容都存进file2,有两种办法:
1 循环执行行操作
2 read命令
这里主要讲一下read command,read不是逐行操作的,可以直接对整个文件操作,具体如下:
set fid1 [ open file1 r];
set fid2 [ open file2 w];
set tmp [ read $fid1 ]
puts $fid2 $tmp
close $fid1; close $fid2
进行完上述操作后,再打开file2就是下面的内容了
上面的操作只是简单的读写,无论是对于gets这种逐行操作,还是read这种可以对file整体操作,得到数据之后除了直接存进文件,还可以对数据进行各种处理。
下面就举一个具体的应用:
我们常常要分析congestion问题,常用到的command的是
reportCongestion -hotspot > DrawHotSpots.txt
获得的是
这个时候我们可能需要根据表中给出的坐标去定位hotspot点,再去分析congestion等相关问题,但是通过文件处理,我们可以再gui上直接画出来这些hotspot点,十分直观,先上效果图
是不是很省力又很直观。那么这是怎样通过脚本来实现的呢?除了上面提到的部分,还有几个核心command
eof fileId ;# 如果到达fileId标识的文件的末尾返回1,否则返回0。
string first string1 string2 ?startindex?
# 在string2 中从头查找与string1匹配的字符序列,如果找到,那么就返回匹配的第一个字母所在的位置(0-based)。如果没有找到,那么返回-1。如果给出了startindex变量,那么将从startindex处开始查找
>> string first ab defabc 3; >> 3
>> string first ab defabc 4; >> -1
下面是全部代码
proc DrawHotSpots { { filename "" } } {
global env
set hotspotfilename $filename
if { $hotspotfilename == "" } {
if { [ is_common_ui_mode ] } {
eval_legacy { reportCongestion -hotspot > DrawHotSpots.txt }
} else {
reportCongestion -hotspot > DrawHotSpots.txt
}
set hotspotfilename "DrawHotSpots.txt"
} elseif { [ file exists $filename ] == 0 } {
puts "ERROR: Cannot find hot spot file named filename"
return
}
set f [ open $hotspotfilename "r" ]
gets $f buf
while { ( [ eof $f ] == 0 ) && ( [ string first " top " $buf ] == -1 ) } {
gets $f buf
}
set numhotspots 0
set topndx [ string first " top " $buf ]
if { $topndx == 9 } {
scan $buf "%s %s %i" tmp1 tmp2 numhotspots
gets $f buf
gets $f buf
gets $f buf
for { set i 1 } { $i <= $numhotspots } { incr i } {
gets $f buf
set hotspotnum -1
set x1 -1.0
set y1 -1.0
set x2 -1.0
set y2 -1.0
set score -1.0
scan $buf "%s %s %i %s %f %f %f %f %s %f" tmp1 tmp2 hotspotnum tmp3 x1 y1 x2 y2 tmp4 score
createMarker -tool reportCongestion -type HotSpot -bbox [ list $x1 $y1 $x2 $y2 ] -desc "Congestion Hotspot - Score: $score "
gets $f buf
}}
close $f }
除了上述例子,掌握文件处理在日常工作中还有非常多的用途,起到更大作用,希望能对和我一样的朋友有一点点启发。
欢迎关注:IC菜鸟上分记