0

我的 Perl 代码需要帮助。

我想搜索一个文件并逐行读取它,如果电话号码的父级匹配,它必须将该行打印到另一个文件。

我可以匹配一个字符串,但不确定:如何匹配电话号码的格式/模式电话号码可以不同。我只想遵循电话号码匹配的格式。

数字的示例可以是 xx-xxx-xxxx

这是我的代码看看

#!//usr/bin/perl

# Saving The Aurgument Values To Local Variables Sent from the Command line

$source         = $ARGV[0];
$pattren        = $ARGV[1];
$destination        = $ARGV[2];

$matches        = 0;


# Using open function to open the file and the pointer will be save to variable $fh
# die condition used with a message is the file name give from the command line could
# doesn't exsist

open($sr, '<', $source) or die "Could not open file $source ";
open($ds, '>', $destination);

# Using While loop condition as long as we are getting data per line.

while ($line = <$sr>){ #While reading one line at a time from the log....

  if ($line =~ m/$pattren/i) {   
        #Printing the Mached line Content Of the source File to the destination the File.
        print $ds $line;
        $matches++;
  }
}

close $ds;
close $sr;

print "\n";
print "Macthes Found = ". $matches. "\n";
print "Reading from File    = " . $source . "\n";
print "Writing it to File   = " . $destination . "\n";



print "\n";

# End of file extract
4

1 回答 1

3
>> Example of Number can be xx-xxx-xxxx

那么你的正则表达式可能是

\b\d{2}-\d{3}-\d{4}\b

因此,使用以下命令运行您的 perl 脚本

test.pl infile '\b\d{2}-\d{3}-\d{4}\b' outfile
于 2013-11-10T16:29:17.393 回答