我的Perl代码需要帮助。
我想搜索一个文件并逐行阅读,如果电话号码的父级匹配则必须将该行打印到另一个文件。
我能够匹配字符串但不确定:如何匹配电话号码的格式/模式 电话号码可以不同。我只想按照电话号码匹配的格式。
Number的示例可以是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
答案 0 :(得分: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