如何使用 awk/sed 更改小数点分隔符?

发布于 2024-09-13 15:57:22 字数 119 浏览 3 评论 0原文

如何使用 sedawk 将数字格式(不同的小数点分隔符)从 XXXXXX.XXX 更改为 XXXXXX,XXX

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

旧故 2024-09-20 15:57:22

你想要有多严格?您可以更改所有 . 字符,正如其他人所建议的那样,但是如果您有的不仅仅是数字,这将导致很多误报。更严格一点的是要求点两边都有数字:

$ 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

这确实允许在一些奇怪的情况下进行更改,即 255.255.255.0a1.1a,但可以干净地处理“正常”数字。

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.

秋叶绚丽 2024-09-20 15:57:22

如果您想出于装饰目的替换小数分隔符

在大多数情况下 tr 可能是替换字符的最简单方法:

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

当然,如果您处理输入混合数字和字符串,您将需要更强大的方法,例如 Michael J. Barber 提出的方法,甚至更多。

如果您想替换小数分隔符以进行计算默认

情况下,gawk(GNU awk,即大多数 GNU/Linux 发行版的 awk)使用点作为小数分隔符:

$ 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

但是您可以使用 --use-lc-numeric 选项强制它使用当前区域设置的小数分隔符

$ 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

如果输入格式与当前语言环境不同,您当然可以暂时重新定义 LC_NUMERIC:(

$ 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

制作人员和其他链接

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)

芯好空 2024-09-20 15:57:22

当OP谈论数字时,这不是更准确吗……以确保它是点之前的前导数字。该文件可以包含 OP 不想替换的其他点。

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

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'
找回味觉 2024-09-20 15:57:22

要仅替换这一行中的小数逗号:

Total,"14333,374","1243750945,5","100,00%","100,00%","100,00%",1 639 600,"100,00%"

我使用了反向引用(和 MacOSX,所以我需要 -E 选项):

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' 

导致

Total,"14333.374","1243750945.5","100.00%","100.00%","100.00%",1 639 600,"100.00%"

sed 命令说:“查找 '双引号 digital_1,digit_2 形式的每个字符串,后跟一个或零%,双引号'并将其替换为first_match.second_match。”

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

银河中√捞星星 2024-09-20 15:57:22

我认为

s/\./,/g

应该满足你想要的...除非你想要更特别的东西...

I think

s/\./,/g

should serve what u want... unless u want something more special...

七色彩虹 2024-09-20 15:57:22

如果你有 bash/ksh 等

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

if you have bash/ksh etc

var=XXX.XXX
echo ${var/./,}
爱人如己 2024-09-20 15:57:22

你可以这样做:

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

You could do this:

echo "XXX.XX" | sed s/\./,/g
吾家有女初长成 2024-09-20 15:57:22

由于问题也被标记为 awk

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

Since the question is also tagged awk:

awk 'gsub(/\./,",")||1'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文