我会用几个 grep 命令和一个 awk 来做到这一点,所有这些都通过管道连接在一起。我将引导您了解我的逻辑:
1) 使用pcregrep
而不是 grep 来匹配以 "Running" 开头并以 "Tests run: 0" 结尾的多行模式,如下所示:
命令:
pcregrep -M "Running(\n|.)*?Tests run: 0" file.txt
(注意使用-M
参数来允许多行匹配和?
星号之后使其不贪婪)
输出:
[mvn] Running com.mypackage.MyTest
...
[mvn] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 104.648 sec
[mvn] Running com.mypackage.MyNotExecutedTest
...
[mvn] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.525 sec
[mvn] Running com.mypackage.AnotherNotExecutedTest
...
[mvn] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.569 sec
2)如您所见,不幸的是,这也匹配了一些不需要的项目,所以我会再次使用 pcregrep 删除有问题的条目,如下所示:
命令:
pcregrep -M "Running(\n|.)*?Tests run: 0" file.txt | \
pcregrep -Mv "Running(\n|.)*?Tests run: [^0]"
(注意在第二个 pcregrep 命令中使用-v
参数和[^0]
字符类来仅消除运行非零测试次数的进程)
输出:
[mvn] Running com.mypackage.MyNotExecutedTest
...
[mvn] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.525 sec
[mvn] Running com.mypackage.AnotherNotExecutedTest
...
[mvn] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.569 sec
3)然后我会找出包含“正在运行”的行:
命令:
pcregrep -M "Running(\n|.)*?Tests run: 0" file.txt | \
pcregrep -Mv "Running(\n|.)*?Tests run: [^0]" | \
grep -i running
输出:
[mvn] Running com.mypackage.MyNotExecutedTest
[mvn] Running com.mypackage.AnotherNotExecutedTest
4)最后使用 awk 仅打印我有兴趣看到的变量(进程名称,在您的示例中,它似乎总是行中的第三个“单词”):
最终命令:
pcregrep -M "Running(\n|.)*?Tests run: 0" file.txt | \
pcregrep -Mv "Running(\n|.)*?Tests run: [^0]" | \
grep -i running | \
awk '{print $3};'
最终输出:
com.mypackage.MyNotExecutedTest
com.mypackage.AnotherNotExecutedTest
嗯!