If you want to replace the decimal separator with cosmetic purposes
In most cases, tr is probably the easiest way to replace characters:
$ echo "0.3"|tr '.' ',' 0,3
Of course, if you are doing introductory mixes and strings, you will need a more robust approach, such as that suggested by Michael J. Barber or even more.
If you want to replace the decimal separator for calculation purposes
By default, gawk (GNU awk , i.e. awk most GNU / Linux distributions) uses the period as a decimal separator:
$ echo $LC_NUMERIC fr_FR.UTF-8 $ echo "0.1 0.2"|awk '{print $1+$2}' 0.3 $ echo "0,1 0,2"|awk '{print $1+$2}' 0
However, you can force it to use the decimal separator of the current locale with the --use-lc-numeric option:
$ echo $LC_NUMERIC fr_FR.UTF-8 $ echo "0.1 0.2"|awk --use-lc-numeric '{print $1+$2}' 0 $ echo "0,1 0,2"|awk --use-lc-numeric '{print $1+$2}' 0,3
If the input format is different from the current locale, you can, of course, override LC_NUMERIC temporarily:
$ echo $LC_NUMERIC fr_FR.UTF-8 $ echo "0.1 0.2"|LC_NUMERIC=en_US.UTF-8 awk --use-lc-numeric '{print $1+$2}' 0 $ echo "0,1 0,2"|LC_NUMERIC=fr_FR.UTF-8 awk --use-lc-numeric '{print $1+$2}' 0,3
( Credits and other links )
Skippy le grand gourou
source share