
私は次の行を持っています
scalar TestDmaMac4.sink.udpApp[0] throughput:last 11730.559888477
この行からのみ抽出したいのですが、11730
どうすればよいですかgrep
?小数点以下の数字は無視して小数点以下の数字だけ必要としたいと思います。
(注:{space}{tab}それぞれを区切るシーケンスudpApp[0]
でthroughput:last
始まる数字です11730
。 )
答え1
次の正規表現は、形式のすべての浮動小数点数と一致し、対応する浮動小数点[0-9].[0-9]
数の整数部分を返します。
$ a="scalar TestDmaMac4.sink.udpApp[0] throughput:last 11730.559888477"
$ egrep -o '[0-9]+[.][0-9]' <<<"$a" |egrep -o '[0-9]+[^.]' #First grep will isolate the floating number , second grep will isolate the int part.
11730
$ perl -pe 's/(.*?)([0-9]+)(\.[0-9]+.*)/\2/' <<<"$a" #using the lazy operator ?
11730
$ sed -r 's/(.*[^0-9.])([0-9]+)(\.[0-9]+.*)/\2/' <<<"$a" #sed does not have lazy operator thus we simulate this with negation
11730
テストのために、浮動小数点は先行スペースなしで別の場所にある別の文字列で上記の正規表現を試しました。
$ c="scalar throughput:last11730.559888477 TestDmaMac4.sink.udpApp[0]"
$ egrep -o '[0-9]+[.][0-9]' <<<"$c" |egrep -o '[0-9]+[^.]'
11730
$ perl -pe 's/(.*?)([0-9]+)(\.[0-9]+.*)/\2/' <<<"$c"
11730
$ sed -r 's/(.*[^0-9.])([0-9]+)(\.[0-9]+.*)/\2/' <<<"$c"
11730
答え2
l='scalar TestDmaMac4.sink.udpApp[0] throughput:last 11730.559888477'
read -r -a a <<<"$l"
dc -e "${a[-1]}dX10r^dsa*la/p"
echo "$l" | perl -lane 'print/\d+(?=\.\d+$)/g'
結果
11730
答え3
グレブを使う:
grep -o " [0-9]\{1,\}"
テストするには:
echo "scalar TestDmaMac4.sink.udpApp[0] throughput:last 11730.559888477" | grep -o " [0-9]\{1,\}"
結果:
11730