How to change the decimal separator with awk/sed?

后端 未结 8 1344
萌比男神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:31

    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.

提交回复
热议问题