2

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.

2
  • Probably better addressed in UNIX SE group.
    – mdpc
    Apr 10, 2013 at 16:49
  • Could you provide sample input and desired output?
    – Thor
    Apr 10, 2013 at 17:02

1 Answer 1

0

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).

Not the answer you're looking for? Browse other questions tagged .