两种模式之间的匹配和文件行

时间:2018-12-04 14:31:16

标签: perl multiline

我正在尝试创建一个perl脚本,该脚本将捕获两个Patterns之间的一行并将其存储到新文件中。

这是文件中的行的样子。

Apr 17 15:57:35 bur-7430 fmd: [ID 377184 daemon.error] SUNW-MSG-ID: FMD-8000-CV,
TYPE: Alert, VER: 1, SEVERITY: Minor
Apr 17 15:57:35 bur-7430 EVENT-TIME: Fri Apr 17 15:56:28 EDT 2015
Apr 17 15:57:35 bur-7430 PLATFORM: SUN SERVER X4-4, CSN: 1421NM900G, HOSTNAME: bur-7430
Apr 17 15:57:35 bur-7430 SOURCE: software-diagnosis, REV: 0.1
Apr 17 15:57:35 bur-7430 EVENT-ID: b22c3c73-77d7-4f4e-8030-c589bf057bb9
Apr 17 15:57:35 bur-7430 DESC: FRU '/SYS/HDD0' has been removed from the system.
Apr 17 15:57:35 bur-7430 AUTO-RESPONSE: FMD topology will be updated.

我正在尝试以下代码,但是没有运气。

use warnings;
my $filename = /tmp/fname.txt;
my $fextract;
{
   open my $fh, '<', $filename or die "can not open file -filename.\n$!\n";
   local $/ = undef;
   $fextract = <$fh>;
}

while(
      $fextract =~ m/SUNW-MSG-ID(.*)DESC/gs
     )
{
print "$1\n";
}

脚本运行得很好,但是没有任何结果。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

对您的代码(在文件中带有引号)来说,它对我来说很好用

> perl script.pl 
: FMD-8000-CV,
TYPE: Alert, VER: 1, SEVERITY: Minor
Apr 17 15:57:35 bur-7430 EVENT-TIME: Fri Apr 17 15:56:28 EDT 2015
Apr 17 15:57:35 bur-7430 PLATFORM: SUN SERVER X4-4, CSN: 1421NM900G, HOSTNAME: bur-7430
Apr 17 15:57:35 bur-7430 SOURCE: software-diagnosis, REV: 0.1
Apr 17 15:57:35 bur-7430 EVENT-ID: b22c3c73-77d7-4f4e-8030-c589bf057bb9
Apr 17 15:57:35 bur-7430