I would like to add leading zeros to a string of digits so as to have exactly three digits. Consider for example
echo "hello_1" | sed -E 's/([0-9]+)/00\1/' # hello_001
echo "hello_12" | sed -E 's/([0-9]+)/0\1/' # hello_012
echo "hello_123" | sed -E 's/([0-9]+)/\1/' # hello_123
Now, the issue is that I am using three different expressions here while I get piped to sed several lines at one. Something like
printf "hello_1\nhello_12\nhello_123\n" | sed -E 's/([0-9]+)/0\1/'
hello_01
hello_012
hello_0123
The result is not the one I am looking for. The expected output is
hello_001
hello_012
hello_123
I tried
printf "hello_1\nhello_12\nhello_123\n" | sed -r ":r;s/\b[0-9]{1,$((3-1))}\b/0&/g;tr"
hello_1
hello_12
hello_123
that I got from this post but it does not work and I don't understand how it works.
I am using GNU sed version 4.2.1 on Linux