开发者

How to change the decimal separator with awk/sed?

How to change number format (different decimal separ开发者_C百科ator) from XXXXXX.XXX to XXXXXX,XXX using sed or awk?


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.


Wouldn't this be more accurate as the OP whas talking about numbers.. to make sure it is a leading number before the dot. The document could hold other dots that the OP don't want to substitute.

sed '/[0-9]\./s/\./,/g'


If you want to replace the decimal separator for cosmetic purposes

In most cases tr is probably the easiest way to substitute characters :

$ echo "0.3"|tr '.' ','
0,3

Of course if you deal with input mixing numbers and strings, you will need a more robust approach, like the one proposed by Michael J. Barber or even more.

If you want to replace the decimal separator for computation purposes

By default gawk (GNU awk, i.e. the awk of most GNU/Linux distributions) uses the dot as 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 using 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 redefine 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)


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


if you have bash/ksh etc

var=XXX.XXX
echo ${var/./,}


You could do this:

echo "XXX.XX" | sed s/\./,/g


Since the question is also tagged awk:

awk 'gsub(/\./,",")||1'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜