2017-10-19 90 views
0

我已經嘗試了不同的方法來做到這一點。我需要改造這一點:Bash - 用0填充每個數字使它使用sed 4位數

5 Dias的,3個月前,4個ANOS

67 decimales

naranjas 45,limones 56

66 + 44 = 100。

7777a 7b 88c 777d

分解爲:

0005 Dias的,0003個月前,0004 ANOS

0067 decimales

naranjas 0045,limones 0056

0066 + 0044 = 0100。

7777a 0007b 0088c 0777d

但我不能設法添加零到最後一行w ith我到目前爲止的代碼如下:

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 

我在想什麼?

+1

字母不是分詞。 – 123

回答

1

你可以用perl

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

,或者如果你真的想你應該使用``的[^ 0-9]代替`\ B`,因爲要使用的sed

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