我有一个testResultOuput.txt
,它包含如下行:
[2022.03.03-13.28.59:742][394]LogAutomationController: Error: Test Completed. **Result={Failed}** **Name={MyAwesomeTest}** Path={AutoTests.UnitTests}
[2022.03.03-13.28.59:742][394]LogAutomationController: BeginEvents: AutoTests.UnitTests
[2022.03.03-13.28.59:742][394]LogAutomationController: Error: Expected 'MyAwesomeTest' to be 0.000000, but it was -2147483648.000000 within tolerance 0.000100.
到目前为止,我发现这些线路具有:& grep Result={Failed} testResultOuput.txt
有人能帮忙吗?
我想写一个shell脚本来按Result搜索,并只打印中的Name={}
和Result={}
您可以尝试--only-matching
grep选项,它将只输出匹配的部分:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
例如:
grep --only-matching 'Result={[[:alpha:]]*} Name={[[:alpha:]]*}'
将输出您的示例:
$ grep --only-matching 'Result={[[:alpha:]]*} Name={[[:alpha:]]*}' foo
Result={Failed} Name={MyAwesomeTest}
$
编辑,遵循注释中的不同要求。如果result
和name
由未定义的字符序列分隔,并且{...}
替换为"..."
,例如:
result="Failed" [2022.03.03-13.28.59:742][394]LogAutomationController: Error: Test Completed. name="MyAwesomeTest"
我会使用sed
而不是grep
。类似于:
sed -nE 's/(bresult="[[:alpha:]]*").*(bname="[[:alpha:]]*")/1 2/p'
这个怎么样:
$ grep -o '<Result={[^}]*} Name={[^}]*}' testResultOutput.txt
Result={Failed} Name={MyAwesomeTest}