如何从这个字符串中获取一些值?
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
在此示例中,我需要总成本 (8) 和扩展节点 (183) 的值。 (也许在另一次执行中我有 47 个。)
我尝试了 ${str:119:1}
,但有时值会更改为 10 或 100,而我只能得到一位数字。我需要完整的值。
最佳答案
grep
可以帮到你
例子
$ grep -oP '(nodes expanded:|total cost of) [0-9]+' inputFile
total cost of 8
nodes expanded: 183
或
如果你不知道字符串是什么
$ grep -oP '(nodes expanded:|total cost of) \K[0-9]+' input
8
183
关于linux - 提取复杂字符串的片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080230/