Regex 在perl中根据条件从日志文件中提取特定URL

Regex 在perl中根据条件从日志文件中提取特定URL,regex,perl,Regex,Perl,我有这个日志文件,我必须提取仅通过测试用例的REQ-URL(实际上它有2行长)。我如何首先检查它是否是一个通过的测试用例,然后提取它?这个列表有10页长。请有人帮我解决这个问题。我被这个问题困扰了一段时间 [Case MostPopular-BlogFlag] [REQ-URL]: http://hostname:8080/guiderest? customerId=cisco&code=news&guide=MostPopular&attrFilter=BlogFla

我有这个日志文件,我必须提取仅通过测试用例的REQ-URL(实际上它有2行长)。我如何首先检查它是否是一个通过的测试用例,然后提取它?这个列表有10页长。请有人帮我解决这个问题。我被这个问题困扰了一段时间

[Case MostPopular-BlogFlag] 
[REQ-URL]: http://hostname:8080/guiderest?
customerId=cisco&code=news&guide=MostPopular&attrFilter=BlogFlag:true&v=1
***Passed!***
  .
  .
  .
[Case MostPopular-BlogFlag] 
[REQ-URL]: http://hostname:8080/guiderest?
customerId=cisco&code=news&guide=MostPopular&attrFilter=BlogFlag:true&v=1
***Failed!***

提前谢谢你

您必须实现一个基本状态机

if ( $line =~ /REQ_URL/) {
    $maybe_line = $line ;
    $append = 1 ;
} elsif ( $line =~ /\*\*\*(Passed|Failed)/ {
      if ( $1 =~ /Passed/ ) {
         output_line($maybe_line,$line) ;
      }
      $append = "" ; 
      $maybe_line = "" ; 
} else {
    if ( $append ) {
       $maybe_line .= $line
    }
} 

您可以这样做:

#!/usr/bin/perl

use strict;

my $string = '[Case MostPopular-BlogFlag1] 
[REQ-URL]: http://hostname:8080/guiderest?
customerId=cisco&code=news&guide=MostPopular&attrFilter=BlogFlag:true&v=1
***Passed!***
  .
  .
  .
[Case MostPopular-BlogFlag] 
[REQ-URL]: http://hostname:8080/guiderest?
customerId=cisco&code=news&guide=MostPopular&attrFilter=BlogFlag:true&v=1
***Failed!***';

while($string =~ /\[Case\h+(?<case>[^]]+)]\s*\[REQ-URL]:\h+(?<url>\S+(\?\R\S+)?)\s*\*+Passed!\*+/g) {
    print $+ {case} . "\n" . $+ {url} . "\n\n";
}
#/usr/bin/perl
严格使用;
my$string='[案例MostPopular-BlogFlag1]
[REQ-URL]:http://hostname:8080/guiderest?
customerId=cisco&code=news&guide=MostPopular&attrFilter=bloglag:true&v=1
***通过了***
.
.
.
[案例最流行的博客标志]
[REQ-URL]:http://hostname:8080/guiderest?
customerId=cisco&code=news&guide=MostPopular&attrFilter=bloglag:true&v=1
***失败!***';
while($string=~/\[Case\h+(?[^]]+)]\s*\[REQ-URL]:\h+(?\s+(\?\R\s+)?\s*\*+已通过!\*+/g){
打印$+{case}.\n.$+{url}.\n\n;
}

如果
***通过,模式将失败***
不在后面。

一个选项是将Perl的记录分隔符(
$/
)设置为“[Case MostPopular BlogFlag]”,这样日志将以该字符串分隔的“块”读取。接下来,使用包含“***已通过!”的正则表达式,如果不存在,则获取下一条记录。如果找到,请从捕获的URL中删除任何换行符,然后打印:

use strict;
use warnings;

local $/ = '[Case MostPopular-BlogFlag]';

while (<>) {
    next unless my ($url) = /\[REQ-URL\]:\s+([^*]+)\*\*\*Passed!/;
    $url =~ s/\n//g;
    print "$url\n";
}
使用严格;
使用警告;
本地$/='[Case MostPopular BlogFlag]';
而(){
下一步除非我的($url)=/\[REQ-url\]:\s+([^*]+)\*\*\*已通过!/;
$url=~s/\n//g;
打印“$url\n”;
}
用法:
perlscript.pl infle[>outFile]

最后一个可选参数将输出定向到文件


希望这有帮助

谢谢你的快速回复。这里的主要问题是,我也使用相同的正则表达式($line=~/REQ_URL/),但是它只提取那一行,但是如果您看到代码,URL也会在下一行继续。我怎么知道?我尝试使用($line=~/REQ\u URL/s),//s where's'修饰符检查换行符,但它会读取以下所有行。我只想读下一行。请给出建议。谢谢你的回复。您的代码确实提取了URL,但它只提取了REq URL的第一行,但我们必须获得整个URL,它也位于[REq-URL]的下一行,那么这将如何工作???非常感谢您的快速回复。我试着运行你的代码,但它没有显示任何输出。@user2457969:我已经测试过了,它可以工作了。请参阅我编辑的答案和工作示例。您是否测试了给出的任何答案?