linux - 提取复杂字符串的片段

标签 linux string bash split

如何从这个字符串中获取一些值?

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/

相关文章:

linux - 如何更新minikube最新版本?

string - O(m*n) 的最长公共(public)子串非 DP 解决方案

linux - 在文件中搜索字符串,然后打印以该字符串开头的行

bash - 将文本从一个文件插入到另一个文件的开头 - bash

Linux - 通过覆盖将文件从源移动到目标

c - 如何在 Ubuntu x64 中使用 ptrace 插入 int3?

linux - 如何使用 winscp 控制台将文件从远程 Windows 8 服务器传输到 Linux 服务器

javascript - 将日期字符串转换为数组

C++ Data-Url as String to const byte *

linux - Bash,每次输入无法识别的参数时在终端中打印 0