【问题标题】:Grep number extractionGrep号码提取
【发布时间】:2018-11-08 15:32:54
【问题描述】:

我需要使用 grep 从具有以下格式的一系列行中提取数值:“总差值为:11.7423485766”。

这里是文件的格式

smoothSolver:  Solving for Uax, Initial residual = 0.00117823324374, Final residual = 2.6551659504e-06, No Iterations 4

smoothSolver:  Solving for Uay, Initial residual = 0.00102460810826, Final residual = 2.27551114222e-06, No Iterations 4

GAMG:  Solving for pa, Initial residual = 0.083060886073, Final residual = 4.42348716834e-05, No Iterations 8

Adjoint continuity errors : sum local = 0.000102057393701, global = -2.79342190934e-05, cumulative = -0.00156476995631

The ratio scalar is 0.00767871345324

The toal volume  is 0.01

The percentage of fluid domain  is 0.767871345324

omegaVol is 1.331

Total Pressure at the inlet: 18.3146365858

Total Pressure at the outlet: 0.533553352625

The total difference is: 17.7810832332

ExecutionTime = 1.05 s

我试过了

grep "The total difference is:" | cut -d' ' -f9   log

但它似乎不起作用,我不明白为什么。

有什么想法吗?感谢您的帮助

【问题讨论】:

  • 如果我理解正确,你应该使用 grep "The total difference is:" |剪切 -d' ' -f5 日志
  • 您好@M.Falzone 感谢您的快速回复。也许我应该添加我的文件的组成方式。我刚刚更新了主线程。
  • 您的例句包含 5 个空格分隔的字段,但您正在尝试获取第 9 个字段。这是完整的输入行吗?
  • @BenjaminW。我刚刚更新了格式文件

标签: linux terminal grep


【解决方案1】:

这个呢:

grep "The total difference is:" log | grep -o "[0-9.]*"

第一个grep 占整行,包含匹配的文本,第二个只占数字。

【讨论】:

  • 谢谢你,它有效。无论如何,我也想理解我在某处找到的这种语法:>>>grep 'Solving for Uy' |剪切-d''-f9 | tr -d ','
【解决方案2】:

多米尼克的回答是正确的。只是为了完整起见,您的错误是,您没有为 grep 指定文件。于是你进入了grep的交互模式。

当你这样做时,它会起作用:

grep "The total difference is:" log | cut -d' ' -f5

P.S.:您也选择了错误的字段。是 5,而不是 9。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2017-01-25
    • 2017-03-05
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多