Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using awk to simply change some text in a file, while maintaining the values as they are currently in the file.

The problem is a few of the values are changing. the values are maintained if i use %f - however, i would like to eliminate the unnecessary trailing zero's. This is accomplished for most values when using %g, but %g causes a few of the values to convert to scientific numbers, which i do not want.

Is there a way to both print the numbers to the new file without the trailing zero's as well as not convert any numbers to scientific notation?


When i apply the following to the file i am changing, using %f:

(rline == 4) {printf("%f\t %s\n",$1,"gsurf_intercept")}

it results in:

1000000.000000   gsurf_intercept

when i use the %g option:

(rline == 4) {printf("%g\t %s\n",$1,"gsurf_intercept")}

it results in:

1e+06    gsurf_intercept

what i want is:

1000000     gsurf_intercept

Also, i applying this to 70 lines in the file i'm changing, and i would like the resulting file to produce two columns, each column in alignment. I find that not each item lines up with the one above or below - this happens when the item in the first column is a string and i use %s for it.

share|improve this question
Probably better addressed in UNIX SE group. –  mdpc Apr 10 '13 at 16:49
Could you provide sample input and desired output? –  Thor Apr 10 '13 at 17:02

2 Answers 2

You could convert the floating point to an integer number and print it. There would be no trailing zeros and you could print with format code %d or %ld.

I think that changing %f to %d might do the trick however w/o having to do the conversion explicitly (i.e. let printf do the conversion).

share|improve this answer

The %f format specifier supports a precision argument, so to only output two trailing decimals:

seq 1e6 1e5 2e6 | awk '{ printf "%.2f\n", $1 }'



See for example man 3 printf for more on the format specifiers.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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