How to convert big numbers into scientific notation

numeric datasedtext formatting

I am working on a file where I have columns of very large values.

(like 40 digits : 646512354651316486246729519751795724672467596754627.06843
and so on …)

I would like to have those numbers in scientific notation but with only 3 or 4 numbers after the dot. Is there a way to use sed or something to do that on every number that appears in my file?

Best Answer

If you want to convert only specific column, awk helps

$ cat ip.txt
foo 64651235465131648624672951975 123
bar 3452356235235235 xyz
baz 234325236452352352345234532 ijkls

$ # change only second column
$ awk '{$2 = sprintf("%.3e", $2)} 1' ip.txt
foo 6.465e+28 123
bar 3.452e+15 xyz
baz 2.343e+26 ijkls
Related Question