Regex 使用sed-linux在一行中输出一个带有特定字符串的数字
我有如下输入Regex 使用sed-linux在一行中输出一个带有特定字符串的数字,regex,linux,sed,compare,Regex,Linux,Sed,Compare,我有如下输入 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 我想从这个文件
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。单向:
sed -n 's/.*Maxval=\s*\([^ ]*\).*/\1/p' file.txt
结果:
2.237295e+000
提议:
cat test.txt | grep Maxval | sed-e's/^.*Maxval=*/'-e's/at.*$/'
2.237295e+000
- cat将文件显示到标准输出
- 格雷普只保持有趣的路线
- 第一个sed正则表达式删除行的开头直到空格的结尾
- 第二个sed正则表达式删除“at”,直到行结束
> 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
使用以下一个行程序将仅显示
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也可以使用grep:
<infile grep -o 'Maxval= *[^ ]\+' | grep -o '[^ ]\+$'
根据操作结果,还应包括*Maxval=
不必要的管道cat
和grep
。根据操作结果,还应包括*Maxval=
@sarathi不需要Maxvel。。我只想要那个漂亮的号码。不过谢谢。OP:我的输出将仅为数字2.237295e+000。
您可能想提及-r
标志仅为GNU
<infile grep -o 'Maxval= *[^ ]\+' | grep -o '[^ ]\+$'
2.237295e+000