The
printf "%'d "
solution did not work for me either. I have GNU AWK 3.1.5. The man doesn't mention apostrophe among printf format options, and I don't have thousands.awk file.
This solution
echo 1 12 123 1234 12345 123456 1234567 | awk --re-interval '{print gensub(/([[:digit:]])([[:digit:]]{3})/,"\\1,\\2","g")}'
will not work, because only the #,### pattern gets repeated. It becomes more clear when you add a few longer numbers to the list.
I created the following solution:
#!/bin/sh
nums=`echo -e " 1\n 12\n 123\n 1234\n 12345\n 123456\n 1234567\n 12345678\n 123456789\n 1234567890\n"`
echo "$nums" | awk --re-interval '{
if (length($1) > 3)
{
a = int(length($1)%3)
if (a == 0)
{
p1 = gensub(/([[:digit:]]{3})/, "\\1,", "g")
printf "%-20d %s \n", $1, gensub(/,$/, "\\1", "g", p1)
}
if (a == 1)
{
q1 = gensub(/\<([[:digit:]])/, "\\1,", "g")
q2 = gensub(/([[:digit:]]{3})/, "\\1,", "g", q1)
printf "%-20d %s \n", $1, gensub(/,$/, "\\1", "g", q2)
}
if (a == 2)
{
r1 = gensub(/\<([[:digit:]]{2})/, "\\1,", "g")
r2 = gensub(/([[:digit:]]{3})/, "\\1,", "g", r1)
printf "%-20d %s \n", $1, gensub(/,$/, "\\1", "g", r2)
}
}
}'
Note! This will not work with non-integers (i don't need it for my script), but it can be extended with some effort!