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?

Examples:

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

closed as off-topic by A.B., Jan, Eric Carvalho, Pabi, Maythux May 13 at 6:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Jan, Eric Carvalho, Pabi, Maythux
If this question can be reworded to fit the rules in the help center, please edit the 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

1 Answer 1

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

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