Wednesday, April 13, 2022

[SOLVED] trying to find a way to bash script my results

Issue

I have a testResultOuput.txt which contains lines that look like this:

[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. 

and so far I find these lines with: & grep Result={Failed} testResultOuput.txt

anyone can help?

I want to write a shell script to search by Result and print only the Name={} and Result={} from these lines


Solution

You may try --only-matching grep option, which will output only matched part :

-o, --only-matching
       Print only the matched (non-empty) parts  of  a  matching  line,
       with each such part on a separate output line.

For example:

grep --only-matching 'Result={[[:alpha:]]*} Name={[[:alpha:]]*}'

will output with your example:

$ grep --only-matching 'Result={[[:alpha:]]*} Name={[[:alpha:]]*}' foo
Result={Failed} Name={MyAwesomeTest}
$

EDIT, following different requirement in comments. If result and name are separated by undefined sequence of characters, and {...} replaced with "...", for example :

result="Failed" [2022.03.03-13.28.59:742][394]LogAutomationController: Error: Test Completed. name="MyAwesomeTest"

I would use sed instead of grep. Something like :

sed -nE 's/(\bresult="[[:alpha:]]*").*(\bname="[[:alpha:]]*")/\1 \2/p'


Answered By - Bruno
Answer Checked By - Pedro (WPSolving Volunteer)