Regexp用于解析测试输出并仅打印故障情况

时间:2018-04-12 23:38:37

标签: regex perl grep

我试图使用正则表达式解析单元测试的输出日志。 我的目标是只打印错误案例的细节。

以下是输出示例:

[ RUN      ] testname 1
Success log
[       OK ] testname 1
[ RUN      ] testname 2
Failure details 1
Failure details 1
[  FAILED  ] testname 2
[ RUN      ] testname 3
Success log
[       OK ] testname 3
[ RUN      ] testname 4
Failure details 2
Failure details 2
[  FAILED  ] testname 4
[ RUN      ] testname
Success log
[       OK ] testname

输出应为:

[ RUN      ] testname 2
Failure details 1
Failure details 1
[  FAILED  ] testname 2
[ RUN      ] testname 4
Failure details 2
Failure details 2
[  FAILED  ] testname 4

这是我到目前为止所尝试的内容:

grep -Pzo "(?s)^\[ RUN      \].*?^\[  FAILED  \].*?$" test.log

但是,输出不正确,因为序列.*?不会排除[ OK ]

在练习中,我得到两场比赛:

1

[ RUN      ] testname 1
Success log
[       OK ] testname 1
[ RUN      ] testname 2
Failure details 1
Failure details 1
[  FAILED  ] testname 2

2

[ RUN      ] testname 3
Success log
[       OK ] testname 3
[ RUN      ] testname 4
Failure details 2
Failure details 2
[  FAILED  ] testname 4

我想我应该使用一种名为Negative Lookahead的东西,但我无法让它发挥作用。

你有什么建议吗?

4 个答案:

答案 0 :(得分:3)

$ perl -ne 'BEGIN{$/="[ RUN "} chomp; print $/,$_ if /FAILED/' ip.txt
[ RUN      ] testname 2
Failure details 1
Failure details 1
[  FAILED  ] testname 2
[ RUN      ] testname 4
Failure details 2
Failure details 2
[  FAILED  ] testname 4
  • BEGIN{$/="[ RUN "}将输入记录分隔符从换行符更改为[ RUN
  • chomp从每条记录中删除记录分隔符
  • print $/,$_ if /FAILED/打印记录分隔符及其包含FAILED
  • 的记录


这类似于

gawk -v RS='\\[ RUN ' -v ORS= '/FAILED/{print "[ RUN " $0}' ip.txt

答案 1 :(得分:1)

试试这个:

SELECT * 
FROM news 
WHERE published='1' 
  AND title LIKE '%$q%' 
   OR details LIKE '%$q%'
ORDER BY id DESC

https://regex101.com/r/i0aacp/2/

答案 2 :(得分:1)

你可以这样做:

(.*\d\n(?:F.*\n)+.*\d)

这会捕获以数字(\d\n)结尾的行,后跟一个或多个F.*\n的实例。

答案 3 :(得分:1)

使用Perl

perl -0777 -ne'print grep /Failure/, split /^(?=\[ RUN)/m' test.log

使用test.log

中提供的示例打印所需的输出

-0777启用" slurp"模式,因此整个文件被读入$_。这是^上的split,匹配的行以/m开头,lookahead表示模式[ RUNsplit返回的列表通过grep传递,该Eigen传递带有Failure的块,并打印此输出列表。