Perl脚本 - 查看每行的第一个数字,并在0存在时发出警报

时间:2016-06-22 15:51:24

标签: regex perl

目前,脚本只查看txt文件的第一个字符,如果使用正则表达式,该值为0,则发送电子邮件。我正在尝试更新脚本,以便它查看每一行,直到文件结束,并且任何行的警报都具有数字0.如果所有行都有1则不执行任何操作。任何帮助将不胜感激。

警报示例

1
1
1
0 -since there is a 0 an email alert would be generated 
1
1

以下代码:

use warnings;
use strict;

my $file = '/users/tneal01/SPOOL/output.txt';
my $mark = 0;


my $cont = do {
    open my $fh, '<', $file  or die "Can't open $file -- $!";
    local $/;
    <$fh>;
};

# Pull the first number
my ($num) = $cont =~ /^(\d+)/;

if ($num == $mark)
{
    my $body = "status $num has been recorded ";
    my $cmd_email = "echo $body | " .
        "mailx -s \"error occurring\" tneal01\@gmail.com";
    system($cmd_email) == 0  or die "Error sending email -- $!";
}

4 个答案:

答案 0 :(得分:3)

我可能会选择以下内容:

#!/usr/bin/env perl
use strict;
use warnings;

my $file   = '/users/tneal01/SPOOL/output.txt';
my $mark   = '0';
my $search = qr/^$mark\b/;


open my $fh, '<', $file or die "Can't open $file -- $!";

while (<$fh>) {

   #line starts with 0. Or check other regex.
   if (m/$search/) {
      my $body = "status $mark has been recorded ";
      my $cmd_email =
        "echo $body | " . "mailx -s \"error occurring\" tneal01\@gmail.com";
      system($cmd_email) == 0 or die "Error sending email -- $!";

     #bail out the loop - assume you don't want more than one email per thing.
      last;
   }
}

close ( $fh );

答案 1 :(得分:2)

此解决方案一次只读取一行...有简单的解决方案,但需要将整个文件加载到内存中... 我还假设你想知道文件中有多少个美元标记。

$("body").on("click", "button", function() {
    audioelement[0].play();
});

答案 2 :(得分:0)

这与其他解决方案有所不同。

  1. 它从命令行上给出的任何文件名读取
  2. 一旦发现第一个错误,它就会停止检查
  3. 我不知道这些改进对你有多大帮助。

    use warnings;
    use strict;
    
    my $mark = 0;
    
    while (<>) {
      my ($num) = /^(\d)/;
      if ($num == $mark) {
        my $body = "status $num has been recorded ";
        my $cmd_email = "echo $body | " .
            'mailx -s "error occurring" tneal01\@gmail.com';
        system($cmd_email) == 0  or die "Error sending email -- $!";
        last; # stop checking after the first error
      }
    }
    

    (哦,我将一些双引号转换为单引号,这样你就不必转义嵌入式双引号。)

答案 3 :(得分:0)

此版本对现有脚本使用最少的更改。我评论了这些变化。

use warnings;
use strict;

my $file = '/users/tneal01/SPOOL/output.txt';
my $mark = 0;


my $cont = do {
    open my $fh, '<', $file  or die "Can't open $file -- $!";
    local $/;
    <$fh>;
};

# Pull the first number            <-- comment not needed
# my ($num) = $cont =~ /^(\d+)/; # <-- delete this line

if ($cont =~ /^$mark/m)     # <-- change the condition to this regex
{
    my $body = "status $mark has been recorded "; # replace $num with $mark
    my $cmd_email = "echo $body | " .
        "mailx -s \"error occurring\" tneal01\@gmail.com";
    system($cmd_email) == 0  or die "Error sending email -- $!";
}

更改的作用是使用正则表达式检查是否有任何行以$mark中定义的值开头。

我删除了$num变量,但其内容与$mark相同,因此我们只能使用$mark

明细$cont =~ /^$mark/m

  • $cont =~将以下正则表达式应用于$cont
  • 中包含的字符串
  • /启动正则表达式
  • ^匹配行的开头
  • $mark匹配$ mark变量
  • 中指定的字符串
  • /结束正则表达式
  • m标记告诉正则表达式将$cont视为多行字符串(它是)