我试图使用正则表达式解析单元测试的输出日志。 我的目标是只打印错误案例的细节。
以下是输出示例:
[ 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的东西,但我无法让它发挥作用。
你有什么建议吗?
答案 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
答案 2 :(得分:1)
答案 3 :(得分:1)