2

我有如下输入。

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

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

* Maxval=   2.237295e+000 

有人可以建议如何用 linux sed 来做吗?我的输出只会是数字 2.237295e+000。

4

5 回答 5

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. 

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

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

于 2012-11-14T11:01:38.953 回答
2

单程:

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

结果:

2.237295e+000
于 2012-11-14T10:53:08.483 回答
0

提议:

cat test.txt | grep Maxval | sed -e 's/^.*Maxval= *//' -e 's/ at.*$//' 2.237295e+000

  • cat 将文件显示到标准输出
  • grep 只保留有趣的行
  • 第一个 sed 正则表达式删除行首直到空格结束
  • 第二个 sed 正则表达式删除 'at' 直到行尾
于 2012-11-14T10:55:45.543 回答
0
awk '/Maxval=/{print $3}' your_file

测试如下:

> cat temp
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
> awk '/Maxval=/{print $3}' temp
2.237295e+000
于 2012-11-14T11:01:11.217 回答
0

你也可以用 grep 来做:

<infile grep -o 'Maxval= *[^ ]\+' | grep -o '[^ ]\+$'

输出:

2.237295e+000
于 2012-11-14T12:57:50.940 回答