Bash - 使用 sed 在每个数字中填充 0 使其成为 4 位数字

Bash - Fill with 0 every number to make it 4 digits using sed

我尝试过不同的方法。我需要改造这个:

5 dias, 3 meses, 4 anos

67 decimales

naranjas 45, limones 56

66 + 44 = 100.

7777a 7b 88c 777d

进入这个:

0005 dias, 0003 meses, 0004 anos

0067 decimales

naranjas 0045, limones 0056

0066 + 0044 = 0100.

7777a 0007b 0088c 0777d

但是我无法使用目前的代码在最后一行添加零,代码如下:

sed -e 's/\b[0-9]\{3\}\b/0&/g; s/\b[0-9]\{2\}\b/00&/g; s/\b[0-9]\b/000&/g' numbers.txt >output.txt

我错过了什么?

你可以使用 perl

perl -pe 's/\d+/sprintf "%04d",$&/ge' test

或者如果你真的想使用 sed

sed -r ':1;s/([^0-9]|^)([0-9]{1,3})([^0-9]|$)//;t1' file