Bash - 用0填充每个数字使它使用sed 4位数
问题描述:
我已经尝试了不同的方法来做到这一点。我需要改造这一点: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
我在想什么?
答
你可以用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
字母不是分词。 – 123