[英]output a number in a line with specific string using sed linux
我有一個像下面這樣的輸入。
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。
使用以下單行將僅顯示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
。
單程:
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
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
你也可以用grep來做:
<infile grep -o 'Maxval= *[^ ]\+' | grep -o '[^ ]\+$'
輸出:
2.237295e+000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.