3

I have a numeric value, for example 19.3478 or 22.456 or 10. I would like to remove extra decimals if there are more than two decimals, so that there is never more than two decimals after the comma. If the value is 10.0 it should stay as 10.0 or 10, but it should never be "10.". Is this possible with sed?

0

2 Answers 2

6

This should do it:

sed -re 's/([0-9]+\.[0-9]{2})[0-9]+/\1/g' file_name 
0
1

Here's a Perl solution in case you're interested:

perl -pe 's/(\d+\.\d{2})\d+/$1/g' file_name 

You must log in to answer this question.