0
這是我的腳本,用於檢查文件中是否存在版權。使用grep的Perl多行模式匹配
use strict;
use warnings;
use List::Util 'first';
my $filename="sample.txt"; # this file name will be passed-in to the script.
my @fileContent=`cat $filename`;
my $copyrightFound = first { /copyright .* Shakespeare/i } @fileContent;
if (!$copyrightFound) {
print "\nERROR: Copyright missing\n";
exit;
}
#copyright Found
print @fileContent;
if (grep /Copyright (c) \d+ by Bill Shakespeare\nAll rights reserved./,@fileContent) {
print "\nCopyright is good\n";
} else {
print "\nCopyright needs to be fixed\n";
}
打印:
$ perl copyrightCheck.pl
Copyright (c) 2010 by Bill Shakespeare
All rights reserved.
Copyright needs to be fixed
但版權是好的,有沒有更好的辦法來檢查呢?或者我的grep命令有什麼問題?也可以在同一行或下一行出現All rights reserved.
,我可以用\n*
查看一樣嗎?