使用sed linux输出具有特定字符串的行中的数字

ham*_*han 2 regex linux compare sed

我有一个像下面这样的输入.

Curveplot
Time
Maxima of Curve
Part no.
13 #pts=2
* Minval=   0.000000e+000 at time=        0.000000
* Maxval=   2.237295e+000 at time=        0.001000
   0.000000e+000       0.000000e+000
   9.999999e-004       2.237295e+000
endcurve
Run Code Online (Sandbox Code Playgroud)

我想从这个文件中获取最大值,这是Maxval之后的值

* Maxval=   2.237295e+000 
Run Code Online (Sandbox Code Playgroud)

有人可以建议如何用linux sed做到这一点?我的输出只有2.237295e + 000.

Chr*_*our 5

使用以下单行仅显示 2.237295e+000

sed -nr 's/.*Maxval= *([^ ]*).*/\1/p'

正则表达式:

Match:
.*      # match any characters
Maxval= # upto 'Maxval='
 *      # match multiple spaces (that is a space followed by *)
([^ ])  # match anything not a space, use brackets to capture (save this) 
.*      # match the rest of line

Replace with:
\1      # the value that a was captured in the first set of brackets. 
Run Code Online (Sandbox Code Playgroud)

因此,我们有效地将包含该单词的整行替换Maxval=Maxval.

注意:根据sed您可能需要使用的平台和/或实现-E而不是-r.