regex - replace a pattern using sed -


i have file containing lot of text , digits describing numbers < 1 3 digits of accuracy. i'd replace numbers equivalent integer percentages (numbers 0-99).

0.734 -> 73 0.063 -> 6 0.979 -> 97 

it great round properly, not required.

i've tried following , several variants , can't seem 1 match:

sed -e 's/0\.(\d\d)/&/' myfile.txt 

which understand mean, match digit 0, decimal, capture next digits , have sed replace whole match captured part?

even if got work, don't know how handle 0.063 -> 6 case. sure apprecaite helping hand on this.

sed support character class use longer posix name. digits [[:digit:]]. it's shorter write [0-9].

try this:

sed -e 's/0\.([0-9][0-9]).*/\1/;s/^0//' myfile.txt 

the -e flag tells use modern regex. there 2 commands here, separated ;:

s/0\.([0-9][0-9]).*/\1/: put 2 digits following 0 , dot capture group , replace whole string capture group.

s/^0//: remove leading 0 string after above.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -