How to use Awk to format numbers with a thousands separator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












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'









share|improve this question























  • What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
    – steeldriver
    Dec 13 '15 at 18:22






  • 2




    Your sed command doesn't do what you think. It only works with 9-digit numbers...
    – don_crissti
    Dec 13 '15 at 18:28











  • search term stands for "to search on internet to resolve this"
    – Error3735
    Dec 14 '15 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 '15 at 18:29














up vote
7
down vote

favorite












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'









share|improve this question























  • What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
    – steeldriver
    Dec 13 '15 at 18:22






  • 2




    Your sed command doesn't do what you think. It only works with 9-digit numbers...
    – don_crissti
    Dec 13 '15 at 18:28











  • search term stands for "to search on internet to resolve this"
    – Error3735
    Dec 14 '15 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 '15 at 18:29












up vote
7
down vote

favorite









up vote
7
down vote

favorite











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'









share|improve this question















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'






text-processing awk text-formatting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 '15 at 12:02









nwk

653513




653513










asked Dec 13 '15 at 17:51









Error3735

5018




5018











  • What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
    – steeldriver
    Dec 13 '15 at 18:22






  • 2




    Your sed command doesn't do what you think. It only works with 9-digit numbers...
    – don_crissti
    Dec 13 '15 at 18:28











  • search term stands for "to search on internet to resolve this"
    – Error3735
    Dec 14 '15 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 '15 at 18:29
















  • What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
    – steeldriver
    Dec 13 '15 at 18:22






  • 2




    Your sed command doesn't do what you think. It only works with 9-digit numbers...
    – don_crissti
    Dec 13 '15 at 18:28











  • search term stands for "to search on internet to resolve this"
    – Error3735
    Dec 14 '15 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 '15 at 18:29















What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
– steeldriver
Dec 13 '15 at 18:22




What search term are you using? Try "thousands separator". FYI GNU awk has a printf format modifier
– steeldriver
Dec 13 '15 at 18:22




2




2




Your sed command doesn't do what you think. It only works with 9-digit numbers...
– don_crissti
Dec 13 '15 at 18:28





Your sed command doesn't do what you think. It only works with 9-digit numbers...
– don_crissti
Dec 13 '15 at 18:28













search term stands for "to search on internet to resolve this"
– Error3735
Dec 14 '15 at 18:27




search term stands for "to search on internet to resolve this"
– Error3735
Dec 14 '15 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 '15 at 18:29




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 '15 at 18:29










1 Answer
1






active

oldest

votes

















up vote
10
down vote













A locale-independent solution with manual formatting



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



$ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
| 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 "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
| LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000





share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249116%2fhow-to-use-awk-to-format-numbers-with-a-thousands-separator%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    10
    down vote













    A locale-independent solution with manual formatting



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



    $ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
    | 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 "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
    | LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
    1
    10
    100
    1,000
    10,000
    100,000
    1,000,000
    10,000,000
    100,000,000





    share|improve this answer


























      up vote
      10
      down vote













      A locale-independent solution with manual formatting



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



      $ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
      | 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 "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
      | LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
      1
      10
      100
      1,000
      10,000
      100,000
      1,000,000
      10,000,000
      100,000,000





      share|improve this answer
























        up vote
        10
        down vote










        up vote
        10
        down vote









        A locale-independent solution with manual formatting



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



        $ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
        | 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 "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
        | LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
        1
        10
        100
        1,000
        10,000
        100,000
        1,000,000
        10,000,000
        100,000,000





        share|improve this answer














        A locale-independent solution with manual formatting



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



        $ printf "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
        | 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 "1n10n100n1000n10000n100000n1000000n10000000n100000000n" 
        | LC_ALL=en_US.UTF-8 awk ' printf("%'"'"'dn", $0) '
        1
        10
        100
        1,000
        10,000
        100,000
        1,000,000
        10,000,000
        100,000,000






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 14 at 5:33









        Bruno Bronosky

        1,85511112




        1,85511112










        answered Dec 13 '15 at 18:26









        nwk

        653513




        653513



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249116%2fhow-to-use-awk-to-format-numbers-with-a-thousands-separator%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay