用perl实现的.
print "input file:";
$file=<STDIN>;
chomp($file);
open (Hand,$file)||die "can not open file";
print "input the string to search:";
$str=<STDIN>;
chomp($str);
print "the result:/n";
$i=0;
while (<Hand>)
{
$i++;
while (/$str/g)
{
print "line".$i.": ".$_;
}
}
保存为search.pl
C:/>perl search.pl
input file:hello.txt
input the string to search: hello
the result:
line2: this is a hello test.
lien5: helloworld.
这个程序运行特别快.
print "input file:";
$file=<STDIN>;
chomp($file);
open (Hand,$file)||die "can not open file";
print "input the string to search:";
$str=<STDIN>;
chomp($str);
print "the result:/n";
$i=0;
while (<Hand>)
{
$i++;
while (/$str/g)
{
print "line".$i.": ".$_;
}
}
保存为search.pl
C:/>perl search.pl
input file:hello.txt
input the string to search: hello
the result:
line2: this is a hello test.
lien5: helloworld.
这个程序运行特别快.
博客展示了用Perl实现的文件字符串搜索程序。程序先让用户输入文件名,打开文件,再让用户输入要搜索的字符串,最后遍历文件查找该字符串并输出包含它的行。程序运行速度快。

被折叠的 条评论
为什么被折叠?



