13

Can anyone tell me how to use the awk command for this statement "the number will contain commas after every three digits. For example, 100000000 will become 100,000,000."

I know how to use sed command to obtain this output but I don't know how to use awk, I'm beginner please tell me I'm searching from morning but I didn't get a suitable answer for this so please suggest me best tutorials for learning.

sed command to get the above output:

echo "100000000" | sed -E 's/([0-9]{3})/,\1/2g'
4
  • 2
    What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier Dec 13, 2015 at 18:22
  • 2
    Your sed command doesn't do what you think. It only works with 9-digit numbers... Dec 13, 2015 at 18:28
  • search term stands for "to search on internet to resolve this"
    – Error3735
    Dec 14, 2015 at 18:27
  • Alright! i have seen it'll work just for 9 digit numbers and i want a same output for same numbers but by using the awk can you help me?
    – Error3735
    Dec 14, 2015 at 18:29

3 Answers 3

22

A locale-independent solution with manual formatting

This should work on any POSIX-compatible OS regardless of the locales installed.

$ printf "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n" \
| awk '{ len=length($0); res=""; for (i=0;i<=len;i++) { res=substr($0,len-i+1,1) res; if (i > 0 && i < len && i % 3 == 0) { res = "," res } }; print res }'
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000

A solution that uses printf and the en_US locale.

The printf sequence %'d prints a decimal integer formatted with the current locale's thousands separator.

$ printf "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n" \
| LC_ALL=en_US.UTF-8 awk '{ printf("%'"'"'d\n", $0) }'
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
0
4
    gawk 'END {
            printf("%-20s %\47 10i \n",  "Number of records: ", NR)
            printf("%-20s %\47 10.2f \n",  "Number of records: ", NR)
        }' /var/log/kern.log

    Number of records:        2,818
    Number of records:     2,818.00 `
1

Another solution is numfmt (which is part of coreutils and is included in Debian by default)

$ printf "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n" \
| LC_ALL=en_US.UTF-8 numfmt --padding=12 --grouping
           1
          10
         100
       1,000
      10,000
     100,000
   1,000,000
  10,000,000
 100,000,000
$ printf "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n" \
| LC_ALL=en_US.UTF-8 numfmt --grouping
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000

You can also format the values directly with %'f instead of using --grouping

$ printf "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n" \
| LC_ALL=en_US.UTF-8 numfmt --format "%'f"
$ printf "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n" \
| LC_ALL=en_US.UTF-8 numfmt --format "%'12f"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .