I saw this post, but I was wondering if there is a simpler command to round decimal numbers.
I have a file with milions of numbers:
0.1
1.2
3.8
I want to round them to integer
0
1
4
Is there a simple command to do that?
I saw this post, but I was wondering if there is a simpler command to round decimal numbers.
I have a file with milions of numbers:
0.1
1.2
3.8
I want to round them to integer
0
1
4
Is there a simple command to do that?
You can use the following perl
oneliner:
perl -i -pe 's/(\d*\.\d*)/int($1+0.5)/ge' file
The -i
option will automatically change your decimal numbers in-place.
The regex \d*\.\d*
will ensure that only such numbers will be changed in your original file (i.e other strings will be left untouched)
With the awk
commands in the link you post, you'd get something like this:
awk '{printf("%d\n",$0+0.5)}' file
Or simpler, use:
awk '{printf("%.f\n",$0)}' file
I can come up with nothing easier than that ;)