Bash: Find number in strings and add them

I have a text file title 'results' and it contains

[  PASSED  ] 11 tests.
[  PASSED  ] 590 tests.
[  PASSED  ] 1231 tests.
[  FAILED  ] 4 tests.
[  FAILED  ] 500 tests.

I would like to add the PASSED tests and store into a variable. Add the FAILED tests, add them and store into another variable.

How can I do this?

Answers


One quick way using awk.

Assuming your test output is in a file called test.out:

#!/bin/bash
npasses=$(<test.out awk '
/ PASSED / { total += $4 }
END { print total }')

echo number of passing tests: $npasses

<test.out means awk reads from test.out.

/ PASSED / { total += $4 } appends the forth field to a variable called total, but only for lines matching the regex PASSED.

END { print total } runs at end of file, and prints the value stored in total.


Need Your Help

Plot a pandas dataframe with vertical lines

python plot pandas

I want to plot a dataframe where each data point is not represented as a point but a vertical line from the zero axis like :

Android ListView Adapter OnClickListener issue

android

I created a ListView with a custom layout for each view. I have several onClickListeners within the list view adapter for TextView items within the custom layout. The onClickListeners work within the