How to change number format (different decimal separator) from XXXXXX.XXX
to XXXXXX,XXX
using sed
or awk
?
if you have bash/ksh etc
var=XXX.XXX
echo ${var/./,}
Since the question is also tagged awk
:
awk 'gsub(/\./,",")||1'
To substitute only the decimal commas in this line:
Total,"14333,374","1243750945,5","100,00%","100,00%","100,00%",1 639 600,"100,00%"
I used back-references (and MacOSX, so I need the -E option):
echo 'Total,"14333,374","1243750945,5","100,00%","100,00%","100,00%",1 639 600,"100,00%"' | sed -E 's/("[0-9]+),([0-9]+%?")/\1\.\2/g'
resulting in
Total,"14333.374","1243750945.5","100.00%","100.00%","100.00%",1 639 600,"100.00%"
The sed command says: "Find every string of the form 'double quotes digit_1,digit_2, followed by one or zero %, double quotes' and replace it by first_match.second_match."
I think
s/\./,/g
should serve what u want... unless u want something more special...
How rigorous do you want to be? You could change all .
characters, as others have suggested, but that will allow a lot of false positives if you have more than just numbers. A bit stricter would be to require that there are digits on both sides of the point:
$ echo 123.324 2314.234 adfdasf.324 1234123.daf 255.255.255.0 adsf.asdf a1.1a |
> sed 's/\([[:digit:]]\)\.\([[:digit:]]\)/\1,\2/g'
123,324 2314,234 adfdasf.324 1234123.daf 255,255,255,0 adsf.asdf a1,1a
That does allow changes in a couple of odd cases, namely 255.255.255.0
and a1.1a
, but handles "normal" numbers cleanly.
You could do this:
echo "XXX.XX" | sed s/\./,/g