How to round all numbers in a file

awkmath

How can I round all numbers in a file that countains several columns of numbers up to a certain precision? Can this be done with awk?

A single line looks like this:

text - 0.1655456615 - 0.158645 - 0.846554 - 0.85251 ##

EDIT: '-' is a column delimiter.

Best Answer

It's more easily done with perl:

perl -pe 's/[-+]?\d*(?:\.?\d|\d\.)\d*(?:[eE][-+]?\d+)?/sprintf("%.2g",$&)/ge'
Related Question