用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.
这个程序运行特别快.