帮同事应付考试写的小脚本!觉得挺有用的一段代码,就随手记下来了!
目的:统计目标文件中Success的个数。
tips:
1. 对一行中出现多次关键字,在“/”后使用g来表明是全局查找(贪婪?)
2.用替换的方式便于记录出现的次数
3. 一行中替换的次数作为返回值
#test.log
#what is Success Success;
#the result is error
#there is another Success here
#there are 4 Success in file
#!perl -w
$count = 0;
open(INFILE,"test.log");
while(<INFILE>)
{
$count += $_ =~ s/Success//g;
}
print $count;