Perl if-then-else 逻辑以及如果正则表达式不匹配如何处理无数据
到目前为止,我有一些工作代码进入服务器列表并执行一些正则表达式以从日志文件中获取数据。我想要做的是,如果没有可以从特定服务器上的正则表达式捕获的数据,则显示“没有任何可报告此服务器”的状态。
现在,它会遍历每个服务器,如果数据与正则表达式匹配,它将打印出来。 我添加了一个 else 语句来打印上面的语句来处理这个问题,但现在它为每个不匹配的实例打印它。
这是输出的副本,因为它现在可以工作之前我添加了更改:
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname1
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname2
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: backup-date=20100705003002
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: host=hostname2
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-size=49.75 GB
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-time=00:25:23
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-status=Backup succeeded
========================================================================
这是现在输出的副本,之后我添加了“没有什么可报告的” DATE":(在 while 循环内)。基本上,每次不匹配时它都会打印出这个语句。我真的只是想让它给我一个声明。
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname1
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
...
...
========================================================================
NOTHING TO REPORT FOR THIS DATE...
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: backup-date=20100705003002
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: host=hostname2
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-size=49.75 GB
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-time=00:25:23
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-status=Backup succeeded
这是代码:
# Usage: ./test.pl Ju1 05 2010 <logfilepath> hostname1 hostname2 hostname3
use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
splice(@ARGV, 0, 4, ());
foreach my $server ( @ARGV ) {
print "========================================================================\n";
print "REPORTING SUMMARY for BACKUP SERVER : $server\n";
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ m/.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(host=|ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
# adding else statement here
} else {
print "NOTHING TO REPORT FOR THIS DATE... \n";
}
}
close($fh);
}
So far, I have some working code that goes into a list of servers and does some regex to grab data from a log file. What I want to do is to pring out a status of "NOTHING TO REPORT FOR THIS SERVER" if there is NO data to capture from the regex on a particular server.
Right now, it goes through each server and if data matches the regex, it will print out.
I added an else statement to print out the above statement to handle this, but now its printing it out for every instance it doesn't match.
Here is a copy of the output as it works now BEFORE I added the change:
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname1
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname2
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: backup-date=20100705003002
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: host=hostname2
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-size=49.75 GB
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-time=00:25:23
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-status=Backup succeeded
========================================================================
Here is a copy of the output now, AFTER I added the "NOTHING TO REPORT FOR THIS DATE": (within the while loop). Basically, its printing out this statement everytime it doesn't match. I really just want it to give me one statement.
========================================================================
REPORTING SUMMARY for BACKUP SERVER : hostname1
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
...
...
========================================================================
NOTHING TO REPORT FOR THIS DATE...
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: backup-date=20100705003002
Mon Jul 05 00:30:02 2010: hostname2:backup:INFO: host=hostname2
NOTHING TO REPORT FOR THIS DATE...
NOTHING TO REPORT FOR THIS DATE...
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-size=49.75 GB
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-time=00:25:23
Mon Jul 05 00:55:25 2010: hostname2:backup:INFO: backup-status=Backup succeeded
Here is the code:
# Usage: ./test.pl Ju1 05 2010 <logfilepath> hostname1 hostname2 hostname3
use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
splice(@ARGV, 0, 4, ());
foreach my $server ( @ARGV ) {
print "========================================================================\n";
print "REPORTING SUMMARY for BACKUP SERVER : $server\n";
open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n";
while (my $line = <$fh>) {
if ($line =~ m/.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(host=|ERROR:|backup-date=|backup-size=|backup-time=|backup-status)/) {
print $line;
# adding else statement here
} else {
print "NOTHING TO REPORT FOR THIS DATE... \n";
}
}
close($fh);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这现在有效:
This works now:
我不认为,我做对了。我按照建议(@mcandre)尝试并放置在 while 循环之外,但仍然得到相同的行为。我应该把这张支票放在哪里?
I don't think, I'm getting this right. I tried as suggested (@mcandre) and placed outside of the while loop and still getting the same behavior. Where should I place this check?