Reputation: 11
How can I get some of the values out of this string?
using function abs using problem type ps Path found with total cost of 8 in 0.0 seconds Search nodes expanded: 183 victorious! Score: 502 Average Score: 502.0 Scores: 502.0 Win Rate: 1/1 (1.00) Record: Win
I need the values for total cost (8) and nodes expanded (183) in this example. (Maybe in another execution I have 47.)
I tried ${str:119:1}
, but sometimes the value changes to 10 or 100 and I only get one digit. I need the complete value.
Upvotes: 1
Views: 51
Reputation: 113844
To extract those numbers:
$ echo "$str" | awk -v RS="" '{print $14,$21}'
8 183
If you want to capture those values as conveniently-named shell variables:
$ cost=$(echo "$str" | awk -v RS="" '{print $14}')
$ expanded=$(echo "$str" | awk -v RS="" '{print $21}')
To show that the above worked:
$ echo cost=$cost expanded=$expanded
cost=8 expanded=183
Upvotes: 0
Reputation: 26667
grep
can help you
example
$ grep -oP '(nodes expanded:|total cost of) [0-9]+' inputFile
total cost of 8
nodes expanded: 183
OR
if you dont what the string then
$ grep -oP '(nodes expanded:|total cost of) \K[0-9]+' input
8
183
Upvotes: 2
Reputation: 58808
awk
can split strings into "words" and print them. Example:
$ awk '{ print $3 }' <<< 'using function abs using [...]'
abs
Upvotes: 0