Formatting Numbers with a Thousands Separator Using Awk

Including a comma to separate the thousands in a number can be inconsistent in AIX/Linux.  Here’s an ‘awk’ version that works on both AIX and Linux:

echo "123456789" | LC_ALL="en_US" awk '{printf ("%'"'"'d\n", $1)}'

This displays the number with commas:

123,456,789

In the past, scripts were formatting properly in Linux using this:

LC_ALL=en_US.UTF-8

Using this syntax doesn’t work in AIX, so we went to the universal version. Sometimes the script would work fine when executed from the command-line, but would not work properly when executing from ‘cron’. Using the embedded environmental variable makes it consistent, regardless of how it’s executed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s