I would like to print thousand separator with awk's printf
.
In bash
, it is pretty straightforward:
> printf "a %'d b \n" 1234567
a 1,234,567 b
> LC_NUMERIC=fr_FR.utf8
> printf "a %'d b \n" 1234567
a 1 234 567 b
With an awk
program it almost worked:
> cat > print.awk
{ printf "a %'d b\n",$1}
> echo 1234567 | awk -f print.awk
a 1,234,567 b
(Locale seems to be lost ... )
And in command line:
> echo 1234567 | awk '{printf "%d\n",$1;}'
1234567
> echo 1234567 | awk '{printf "%'d\n",$1;}'
> echo 1234567 | awk '{printf "%\'d\n",$1;}'
Note
I have a workaround, I just want to use %'d
if possible.
Just use a variable to hold '
:
echo 1234567 | q="'" awk '{printf "%"ENVIRON["q"]"d\n", $1}'
1,234,567
or use \047
to represent '
:
echo 1234567 | awk '{printf "%\047d\n", $1}'
This will work in any POSIX awk (not work with mawk
).
With gawk
:
echo 1234567 | awk '{printf "%""\x27""d\n", $1}'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments