Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Perl脚本–;查看每行的第一个数字,如果存在0,则发出警报_Regex_Perl - Fatal编程技术网

Regex Perl脚本–;查看每行的第一个数字,如果存在0,则发出警报

Regex Perl脚本–;查看每行的第一个数字,如果存在0,则发出警报,regex,perl,Regex,Perl,目前,该脚本只查看txt文件的第一个字符,如果该值为0,则使用regex发送电子邮件。我正在尝试更新脚本,以便它查看每一行,直到文件结束,并且任何一行的警报都有数字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/SPOO

目前,该脚本只查看txt文件的第一个字符,如果该值为0,则使用regex发送电子邮件。我正在尝试更新脚本,以便它查看每一行,直到文件结束,并且任何一行的警报都有数字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 -- $!";
}
使用警告;
严格使用;
my$file='/users/tneal01/SPOOL/output.txt';
我的$mark=0;
我的$cont=do{

打开我的$fh,“我可能会选择以下内容:

#!/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 );
!/usr/bin/env perl
严格使用;
使用警告;
my$file='/users/tneal01/SPOOL/output.txt';
我的$mark='0';
我的$search=qr/^$mark\b/;

打开我的$fh,“我可能会选择以下内容:

#!/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 );
!/usr/bin/env perl
严格使用;
使用警告;
my$file='/users/tneal01/SPOOL/output.txt';
我的$mark='0';
我的$search=qr/^$mark\b/;

打开我的$fh,“此解决方案一次只能读取一行…有一些简单的解决方案,但需要将整个文件加载到内存中。。。 我还假设您想知道文件中出现了多少个$mark

#!/usr/bin/perl
use strict;

my $file = 'file.txt';
my $mark = '0';

open my $f, "<$file" or die "Error open file: $!\n";
my $counter=0;
while(my $line = <$f>) {
  if($line =~ /$mark/) {
    $counter++;
  }
}
if($counter) {
    my $body = "status $mark has been recorded $counter times";
    my $cmd_email = "echo $body | mailx -s \"error occurring\" tneal01\@gmail.com";
    system($cmd_email) == 0  or die "Error sending email -- $!";
}
!/usr/bin/perl
严格使用;
my$file='file.txt';
我的$mark='0';

打开我的$f,“此解决方案一次只能读取一行…有一些简单的解决方案,但需要将整个文件加载到内存中。。。 我还假设您想知道文件中出现了多少个$mark

#!/usr/bin/perl
use strict;

my $file = 'file.txt';
my $mark = '0';

open my $f, "<$file" or die "Error open file: $!\n";
my $counter=0;
while(my $line = <$f>) {
  if($line =~ /$mark/) {
    $counter++;
  }
}
if($counter) {
    my $body = "status $mark has been recorded $counter times";
    my $cmd_email = "echo $body | mailx -s \"error occurring\" tneal01\@gmail.com";
    system($cmd_email) == 0  or die "Error sending email -- $!";
}
!/usr/bin/perl
严格使用;
my$file='file.txt';
我的$mark='0';

打开我的$f,“这与其他解决方案有一些不同之处

  • 它读取命令行中给定的任何文件名
  • 一旦发现第一个错误,它将停止检查
  • 我不知道这些改进对你有多有用

    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
      }
    }
    
    使用警告;
    严格使用;
    我的$mark=0;
    而(){
    我的($num)=/^(\d)/;
    如果($num==$mark){
    my$body=“状态$num已记录”;
    我的$cmd_email=“echo$body |”。
    “mailx-s”错误出现在“tneal01\@gmail.com”;
    系统($cmd_email)==0或“发送电子邮件时出错--$!”;
    last;#在第一个错误后停止检查
    }
    }
    

    (哦,我把一些双引号换成了单引号,所以你不必转义嵌入的双引号。)

    这与其他解决方案有一些不同之处

  • 它读取命令行中给定的任何文件名
  • 一旦发现第一个错误,它将停止检查
  • 我不知道这些改进对你有多有用

    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
      }
    }
    
    使用警告;
    严格使用;
    我的$mark=0;
    而(){
    我的($num)=/^(\d)/;
    如果($num==$mark){
    my$body=“状态$num已记录”;
    我的$cmd_email=“echo$body |”。
    “mailx-s”错误出现在“tneal01\@gmail.com”;
    系统($cmd_email)==0或“发送电子邮件时出错--$!”;
    last;#在第一个错误后停止检查
    }
    }
    

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

    此版本对现有脚本使用最小的更改。我已对更改进行了注释

    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 -- $!";
    }
    
    使用警告;
    严格使用;
    my$file='/users/tneal01/SPOOL/output.txt';
    我的$mark=0;
    我的$cont=do{
    
    打开我的$fh,“此版本对现有脚本使用最小的更改。我已对更改进行了注释

    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 -- $!";
    }
    
    使用警告;
    严格使用;
    my$file='/users/tneal01/SPOOL/output.txt';
    我的$mark=0;
    我的$cont=do{
    
    打开我的$fh,'你的程序似乎是一个公平的开始,尽管通常不需要将整个文件读入内存,而且可能会使编码更加困难。你的问题是什么?如果你只是在一行开头寻找
    $match
    的外观,那么我建议你只编写
    If($cont=~/^$match\b/m){#发送电子邮件)
    您的程序似乎是一个不错的开始,尽管通常不需要将整个文件读入内存,而且可能会使编码更加困难。您的问题是什么?如果您只在一行开头寻找
    $match
    的外观,那么我建议您只编写
    If($cont=~/^$match\b/m){#发送电子邮件)
    谢谢塞尔吉奥!我喜欢增加柜台时间!谢谢塞尔吉奥!我喜欢增加柜台时间!谢谢亚当,我知道你是如何包含明细的!没问题。你可以投票选出你认为有用的答案,并接受你使用的答案。我们喜欢这样做,因为我们得到了分数。:-)谢谢亚当,我知道你是如何包含t的他崩溃了!没问题。你可以投票选出你有用的答案,然后接受你所用的答案。我们喜欢这一点,因为我们得到分数。-——如果你发现答案是有用的,请考虑对他们进行投票和接受。如果你发现答案是有用的,请考虑对他们进行投票和接受。