一个在文本文件搜索指定字符串的程序

来源:互联网 发布:js 数组[ ]什么意思 编辑:程序博客网 时间:2024/05/22 10:23
用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.
这个程序运行特别快.
原创粉丝点击