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

vb.net object persisted in database

sql vb.net serialization

How can I go about storing a vb.net user defined object in a sql database. I am not trying to replicate the properties with columns. I mean something along the lines of converting or encoding my ob...

Delphi5 DirectoryExists()

delphi directory delphi-5

I'm using D5 on Win2000 but building applications for all platforms.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.