1

I have a file with data like this

vserver-1 vserver-1_root 0.95 0.0019043 0.948047
vserver-1 home  10.00 8.25 1.75
vserver-1 usr 95 45.65 39.35
vserver-1 file0 100 89.15 10.85

Desired formatted output with awk(rounding off to nearest whole number)

vserver-1 vserver-1_root 1 0 1
vserver-1 home  10 8 2
vserver-1 usr 95 46 39
vserver-1 file0 100 89 11
2

Assuming you have fixed 5 columns file, then you would do:

awk '{printf("%s %s %d %d %d\n",$1, $2, $3+.5, $4+.5, $5+.5)}' infile

This adds 0.5 to the fields then %d will remove the fractional part, resulting in the usual rounding to the nearest integer, with halves (e.g. 2.5) rounded up.

3
  • Here, too, printf("%d", x - .5) doesn't round x down correctly: %d truncates, so if x=1.3, x-.5=.8, then the truncated value is 0.
    – ilkkachu
    May 17 '18 at 7:03
  • as a floor function, it was broken. 1.3 should floor to 1, not to 0. Again, the point is that the float to int conversion done by %d truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the + 0.5 to begin with. If you want to round towards zero to the nearest integer, it's enough to just use printf("%d", x)
    – ilkkachu
    May 17 '18 at 7:20
  • Yes, my bad. you are right May 17 '18 at 7:21
1

using %0.f is simplest way to convert float value to nearest whole number:

awk '{printf ("%s %s %.0f %.0f %.0f\n",$1,$2,$3,$4,$5)}' file
3
  • Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both 1.5 and 2.5 round to 2, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.
    – ilkkachu
    May 17 '18 at 7:07
  • How do I print comma between the values using printf? May 18 '18 at 2:01
  • comma between only values, or replace all spaces with a comma.
    – Siva
    May 18 '18 at 5:38
0

to round up , use +0.5 and print via %d

echo "$number" | awk '{ printf("%d", $1 + 0.5) }'

For your given string ,

vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85

use this command :

awk '{printf "%s %s %d %d %d %s %s %d %d %d %s %s %d %d %d %s %s %d %d %d\n" , $1, $2, $3+0.5, $4+0.5, $5+0.5, $6, $7, $8+0.5, $9+0.5, $10+0.5, $11, $12, $13+0.5, $14+0.5, $15+0.5, $16, $17, $18+0.5, $19+0.5, $20+0.5}' filename
3
  • looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
    – ilkkachu
    May 17 '18 at 6:51
  • Also, you can't round down with printf("%d", x - 0.5). If x is say, 1.3, then x - 0.5 = 0.8, which is truncated to 0. And with + 0.5 you don't get rounding up, but the usual rounding to nearest with .5 going up.
    – ilkkachu
    May 17 '18 at 7:01
  • yeah you are right, this hack works only for going up
    – mkmayank
    May 17 '18 at 7:16

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.