Perl if-then-else 逻辑以及如果正则表达式不匹配如何处理无数据

发布于 2024-09-08 06:22:51 字数 2703 浏览 3 评论 0原文

到目前为止,我有一些工作代码进入服务器列表并执行一些正则表达式以从日志文件中获取数据。我想要做的是,如果没有可以从特定服务器上的正则表达式捕获的数据,则显示“没有任何可报告此服务器”的状态。

现在,它会遍历每个服务器,如果数据与正则表达式匹配,它将打印出来。 我添加了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-09-15 06:22:51
  1. 将布尔值设置为 false。
  2. 在 while 循环内,如果正则表达式匹配,则将布尔值设置为 true。
  3. 在 while 循环之外,如果布尔值仍然为 false,则打印 NOTHING TO REPORT。
  1. Set a boolean to false.
  2. Inside the while loop, if the regex matches, set the boolean to true.
  3. Outside the while loop, if the boolean is still false, print NOTHING TO REPORT.
何时共饮酒 2024-09-15 06:22:51

这现在有效:

use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
my $has_data = 0;
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;
            $has_data=1; #set to true
            #print "Boolean is set to $has_data \n";
        }
    } #end while loop
        if ($has_data==0) {
           print "Boolean is set to $has_data \n";
           print "NOTHING TO REPORT FOR THIS DATE... \n";
        }
       $has_data=0;
       print "Boolean is reset to $has_data \n";
    close($fh);
}

This works now:

use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
my $has_data = 0;
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;
            $has_data=1; #set to true
            #print "Boolean is set to $has_data \n";
        }
    } #end while loop
        if ($has_data==0) {
           print "Boolean is set to $has_data \n";
           print "NOTHING TO REPORT FOR THIS DATE... \n";
        }
       $has_data=0;
       print "Boolean is reset to $has_data \n";
    close($fh);
}
在风中等你 2024-09-15 06:22:51

我不认为,我做对了。我按照建议(@mcandre)尝试并放置在 while 循环之外,但仍然得到相同的行为。我应该把这张支票放在哪里?

use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
my $boolean = 0;
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;
            $boolean=1; #set to true
            #print "Boolean is set to $boolean \n";
        }
    } #end while loop
        if ($boolean=0) {
           print "Boolean is set to $boolean \n";
           print "NOTHING TO REPORT FOR THIS DATE... \n";
        }
    close($fh);
}

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?

use strict;
use warnings;
my($mon,$day,$year,$file) = @ARGV;
my $boolean = 0;
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;
            $boolean=1; #set to true
            #print "Boolean is set to $boolean \n";
        }
    } #end while loop
        if ($boolean=0) {
           print "Boolean is set to $boolean \n";
           print "NOTHING TO REPORT FOR THIS DATE... \n";
        }
    close($fh);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文