Note that all input is treated as strings until it is explicitly converted to how it is used.
OFMT used when strings are converted to numbers, for example:
<<< 0.77767686 awk '{ print 0+$0 }' OFMT='%.2g'
CONVFMT used when numbers are converted to strings, for example:
<<< 0.77767686 awk '{ print "" 0+$0 }' CONVFMT='%.2g'
The conclusion in both cases:
0.78
The latter converts $0 to a number and then combines it with an empty string.
To achieve this for each column, I would suggest using a reasonable setting of input and output record separators:
<<< '0.77767686 0.76555555 0.6667667 0.77878878' \ awk '{ print 0+$0 RT }' CONVFMT='%.2g' RS='[ \t\n]+' ORS=''
Notice the two conversions, first the number with 0+$0 , then back to the string, combining it with RT . RT will be set to the separator of compatible records. Please note that this is specific to GNU awk; for a more portable solution, use a loop, for example:
<<< '0.77767686 0.76555555 0.6667667 0.77878878' \ awk '{ for (i=1; i<=NF; i++) $i+=0 } 1' CONVFMT='%.2g'
The conclusion in both cases:
0.78 0.77 0.67 0.78
source share