gawk printf float pitfall

I had to use a shell script reading in some values and spitting out some values. Those were to be processed by a binary that hung in an endless loop for an unknown reason. The script used the gawk tool excessively and I figured out that there was a problem with the decimal delimiter, which is different in some countries.

sed -e 's/\./,/g'
replacing dots by commas and afterwards vice versa
sed -e 's/,/\./g'
corrected this problem.

But it turned out that tools like gawk do rely on the locales set for your environment. A setenv LANG C before calling the scripts solved the problem even better. Quoted from the GNU awk User's guide
[...] The locale also affects numeric formats. In particular, for awk programs, it affects the decimal point character. The "C" locale, and most English-language locales, use the period character (`.') as the decimal point. However, many (if not most) European and non-English locales use the comma (`,') as the decimal point character.
So when using gawk make sure to execute
setenv LANG C (ksh-style shell) or
export LANG=C (sh-style shell)
in your script before calling gawk. Otherwise it may not run under different localized environments.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?