bash parse output to variable

perlperl
perlperl used Ask the Experts™
on
I am a C++ programmer and little knowledge of bash ;)

I have $output string in format

Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed

I wanted to extract the integer before Failed and assign it to bash variable (in this case 5)
How do i do this in bash script. I want to make sure the value is "0" which means no test failed
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
output='Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed'
F=${output%Failed*}
echo ${F##*,}
Most Valuable Expert 2014
Top Expert 2015
Commented:
Of just to check:
[[ $output =~ ", 0 Failed" ]] && echo "No test failed"

Author

Commented:
Thats a crazy syntax, I would have never figured out this. Can you please provide me the link if you have one

so I am using this now

F=${output%Failed*}
        if [${F##*,} -ne 0 ]; then
                echo "Failed Num of errors = ${F##*,}"
        fi
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
see the Parameter Expansion section of
man bash

Author

Commented:
The second one sounds much easier ;)

Author

Commented:
Is there something called !~

I wanna do and operation

 output='Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed'

 if [ $? -ne 0 ] || [$output !~ ", 0 Failed"]; then
   echo "Failed"
fi

Author

Commented:
somehow this does not work

 if [ $? -ne 0 ] || ! [$output =~ ", 0 Failed"]; then
   echo "Failed"
fi

Author

Commented:
This worked. i hate bash scripting

 if [ $? -ne 0 ] || [[ ! "$output" =~ ", 0 Failed" ]]; then

so particular about spaces.  crazy syntax
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
[[ syntax is slightly different from [ syntax.  In particular, [ does not have the =~ operator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial