How to change the decimal separator with awk/sed?

后端 未结 8 1359
萌比男神i
萌比男神i 2020-12-31 10:40

How to change number format (different decimal separator) from XXXXXX.XXX to XXXXXX,XXX using sed or awk?

8条回答
  •  半阙折子戏
    2020-12-31 11:25

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

提交回复
热议问题