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?


Your shell might have a printf builtin you can use to format numbers.

$ type printf
printf is a shell builtin
$ printf "%.3e\n" 646512354651316486246729519751795724672467596754627.06843
$ printf "%.4e\n" 646512354651316486246729519751795724672467596754627.06843
$ _

If not, there's often a dedicated printf binary, too.

$ which printf
$ _

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.