Perl-文件处理而不使用正则表达式

时间:2015-01-18 05:53:43

标签: perl

我有一段脚本。

if (@ARGV != 2) {

    print "Please pass atleast one paramer\n";
    print "Usage:\n\t $0 <file_name><Pattern>\n";
    exit;
}
$File_name     = $ARGV[0];
$res_File_name = $File_name . "\.result\.txt";
$Pattern       = $ARGV[1]; chomp($Pattern);
open(FD,"$File_name") or die ("File '$File_name' could not be open \n");
open(WFD,">$res_File_name") or die("File $res_File_name could not be opened\n");

print "Enter begin match pattern: ";
$bgn = <stdin>; 
chomp($bgn);

print "Enter end match pattern: ";
$en = <stdin>; 
chomp($en);



while ($line = <FD>) {

    chomp($line);
    if ($line =~ /^$bgn/) { #seaching a patter at begining of the string.

        print "Begin pattern '$bgn' matched with the line '$line'\n";
    }
    if ($line =~ /$en$/) { #seaching a patter at end of the string.

        print "End pattern '$en' matched with the line '$line'\n";

      exit;
    }
print WFD $_ if(/$Pattern/);

}

close(FD);
close(WFD);

我需要执行以下操作      - 在开始模式中,我输入一些东西,在我的文本文件中      - 在最后的模式中我写了一些东西,总的来说我得到了输出

-- What is my aim is to copy those in some result.txt. How i can achieve it.

请在这方面帮助我

1 个答案:

答案 0 :(得分:0)

试试这个

open(nis,"test.txt");
@ar = <nis>;
open(new,">result.txt");
print "Enter the first string : ";
chomp($fis = <>);
print "Enter the second string: ";
chomp($sec = <>);
@vr = map{m/^$fis.*$sec$/g} @ar;
print @vr,"\n";