How to left pad a column in Bash

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











up vote
1
down vote

favorite












I have a bash script that prints the following results:



120,900
1160,001
80,730
600,165
6,310
1111,203


I would like to left pad that so the result will be:



 120,900
1160,001
80,730
600,165
6,310
1111,203


I already use that line to keep just 3 numbers after the comma in the second column



awk 't" "%.3fn", $2 ' MyFile.txt;


How can I accomplish this?







share|improve this question





















  • Please do not post pictures of text; just paste the text.
    – DopeGhoti
    May 9 at 16:10






  • 1




    If you have that awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
    – ilkkachu
    May 9 at 18:44














up vote
1
down vote

favorite












I have a bash script that prints the following results:



120,900
1160,001
80,730
600,165
6,310
1111,203


I would like to left pad that so the result will be:



 120,900
1160,001
80,730
600,165
6,310
1111,203


I already use that line to keep just 3 numbers after the comma in the second column



awk 't" "%.3fn", $2 ' MyFile.txt;


How can I accomplish this?







share|improve this question





















  • Please do not post pictures of text; just paste the text.
    – DopeGhoti
    May 9 at 16:10






  • 1




    If you have that awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
    – ilkkachu
    May 9 at 18:44












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a bash script that prints the following results:



120,900
1160,001
80,730
600,165
6,310
1111,203


I would like to left pad that so the result will be:



 120,900
1160,001
80,730
600,165
6,310
1111,203


I already use that line to keep just 3 numbers after the comma in the second column



awk 't" "%.3fn", $2 ' MyFile.txt;


How can I accomplish this?







share|improve this question













I have a bash script that prints the following results:



120,900
1160,001
80,730
600,165
6,310
1111,203


I would like to left pad that so the result will be:



 120,900
1160,001
80,730
600,165
6,310
1111,203


I already use that line to keep just 3 numbers after the comma in the second column



awk 't" "%.3fn", $2 ' MyFile.txt;


How can I accomplish this?









share|improve this question












share|improve this question




share|improve this question








edited May 9 at 19:17









Jeff Schaller

31.1k846105




31.1k846105









asked May 9 at 16:07









Rania MALK

61




61











  • Please do not post pictures of text; just paste the text.
    – DopeGhoti
    May 9 at 16:10






  • 1




    If you have that awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
    – ilkkachu
    May 9 at 18:44
















  • Please do not post pictures of text; just paste the text.
    – DopeGhoti
    May 9 at 16:10






  • 1




    If you have that awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
    – ilkkachu
    May 9 at 18:44















Please do not post pictures of text; just paste the text.
– DopeGhoti
May 9 at 16:10




Please do not post pictures of text; just paste the text.
– DopeGhoti
May 9 at 16:10




1




1




If you have that awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
– ilkkachu
May 9 at 18:44




If you have that awk snippet in the script, you could probably add the right-alignment there, too, instead of doing it as a separate step in the shell.
– ilkkachu
May 9 at 18:44










3 Answers
3






active

oldest

votes

















up vote
1
down vote













Use printf rather than echo:



$ cat 442817.sh
#!/bin/bash
numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
for n in "$numbers[@]"; do
printf "%10sn" "$n"
done
$ ./442817.sh
120,900
1160,001
80,730
600,165
6,310
1111,203





share|improve this answer



















  • 1




    or replace complete for loop with printf "%8sn" "$numbers[@]"
    – Cyrus
    May 9 at 18:13










  • Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
    – DopeGhoti
    May 9 at 18:14

















up vote
0
down vote













Awk solution:



awk 'FNR == NR len = length; if (len > max_len) max_len = len; next 
printf "%" max_len "sn", $0 ' file.txt file.txt


The output:



 120,900
1160,001
80,730
600,165
6,310
1111,203



Or via GNU coreutils:



printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)





share|improve this answer






























    up vote
    0
    down vote













    $ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
    120,900
    1160,001
    80,730
    600,165
    6,310
    1111,203


    The column output will be



    120 900
    1160 001
    80 730
    600 165
    6 310
    1111 203


    and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.



    If the initial extra two spaces are not wanted, use



    $ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
    120,900
    1160,001
    80,730
    600,165
    6,310
    1111,203





    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f442817%2fhow-to-left-pad-a-column-in-bash%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Use printf rather than echo:



      $ cat 442817.sh
      #!/bin/bash
      numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
      for n in "$numbers[@]"; do
      printf "%10sn" "$n"
      done
      $ ./442817.sh
      120,900
      1160,001
      80,730
      600,165
      6,310
      1111,203





      share|improve this answer



















      • 1




        or replace complete for loop with printf "%8sn" "$numbers[@]"
        – Cyrus
        May 9 at 18:13










      • Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
        – DopeGhoti
        May 9 at 18:14














      up vote
      1
      down vote













      Use printf rather than echo:



      $ cat 442817.sh
      #!/bin/bash
      numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
      for n in "$numbers[@]"; do
      printf "%10sn" "$n"
      done
      $ ./442817.sh
      120,900
      1160,001
      80,730
      600,165
      6,310
      1111,203





      share|improve this answer



















      • 1




        or replace complete for loop with printf "%8sn" "$numbers[@]"
        – Cyrus
        May 9 at 18:13










      • Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
        – DopeGhoti
        May 9 at 18:14












      up vote
      1
      down vote










      up vote
      1
      down vote









      Use printf rather than echo:



      $ cat 442817.sh
      #!/bin/bash
      numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
      for n in "$numbers[@]"; do
      printf "%10sn" "$n"
      done
      $ ./442817.sh
      120,900
      1160,001
      80,730
      600,165
      6,310
      1111,203





      share|improve this answer















      Use printf rather than echo:



      $ cat 442817.sh
      #!/bin/bash
      numbers=(120,900 1160,001 80,730 600,165 6,310 1111,203)
      for n in "$numbers[@]"; do
      printf "%10sn" "$n"
      done
      $ ./442817.sh
      120,900
      1160,001
      80,730
      600,165
      6,310
      1111,203






      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited May 9 at 18:39









      ilkkachu

      48.1k669133




      48.1k669133











      answered May 9 at 16:14









      DopeGhoti

      40k54779




      40k54779







      • 1




        or replace complete for loop with printf "%8sn" "$numbers[@]"
        – Cyrus
        May 9 at 18:13










      • Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
        – DopeGhoti
        May 9 at 18:14












      • 1




        or replace complete for loop with printf "%8sn" "$numbers[@]"
        – Cyrus
        May 9 at 18:13










      • Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
        – DopeGhoti
        May 9 at 18:14







      1




      1




      or replace complete for loop with printf "%8sn" "$numbers[@]"
      – Cyrus
      May 9 at 18:13




      or replace complete for loop with printf "%8sn" "$numbers[@]"
      – Cyrus
      May 9 at 18:13












      Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
      – DopeGhoti
      May 9 at 18:14




      Also viable; the main thrust was to demonstrate printf, as opposed to echo; I just wanted to "show my work" as it were.
      – DopeGhoti
      May 9 at 18:14












      up vote
      0
      down vote













      Awk solution:



      awk 'FNR == NR len = length; if (len > max_len) max_len = len; next 
      printf "%" max_len "sn", $0 ' file.txt file.txt


      The output:



       120,900
      1160,001
      80,730
      600,165
      6,310
      1111,203



      Or via GNU coreutils:



      printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)





      share|improve this answer



























        up vote
        0
        down vote













        Awk solution:



        awk 'FNR == NR len = length; if (len > max_len) max_len = len; next 
        printf "%" max_len "sn", $0 ' file.txt file.txt


        The output:



         120,900
        1160,001
        80,730
        600,165
        6,310
        1111,203



        Or via GNU coreutils:



        printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)





        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          Awk solution:



          awk 'FNR == NR len = length; if (len > max_len) max_len = len; next 
          printf "%" max_len "sn", $0 ' file.txt file.txt


          The output:



           120,900
          1160,001
          80,730
          600,165
          6,310
          1111,203



          Or via GNU coreutils:



          printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)





          share|improve this answer















          Awk solution:



          awk 'FNR == NR len = length; if (len > max_len) max_len = len; next 
          printf "%" max_len "sn", $0 ' file.txt file.txt


          The output:



           120,900
          1160,001
          80,730
          600,165
          6,310
          1111,203



          Or via GNU coreutils:



          printf "%$(sort -nr file.txt | wc -L)sn" $(cat file.txt)






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 9 at 16:32


























          answered May 9 at 16:22









          RomanPerekhrest

          22.4k12144




          22.4k12144




















              up vote
              0
              down vote













              $ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
              120,900
              1160,001
              80,730
              600,165
              6,310
              1111,203


              The column output will be



              120 900
              1160 001
              80 730
              600 165
              6 310
              1111 203


              and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.



              If the initial extra two spaces are not wanted, use



              $ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
              120,900
              1160,001
              80,730
              600,165
              6,310
              1111,203





              share|improve this answer

























                up vote
                0
                down vote













                $ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
                120,900
                1160,001
                80,730
                600,165
                6,310
                1111,203


                The column output will be



                120 900
                1160 001
                80 730
                600 165
                6 310
                1111 203


                and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.



                If the initial extra two spaces are not wanted, use



                $ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
                120,900
                1160,001
                80,730
                600,165
                6,310
                1111,203





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  $ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
                  120,900
                  1160,001
                  80,730
                  600,165
                  6,310
                  1111,203


                  The column output will be



                  120 900
                  1160 001
                  80 730
                  600 165
                  6 310
                  1111 203


                  and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.



                  If the initial extra two spaces are not wanted, use



                  $ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
                  120,900
                  1160,001
                  80,730
                  600,165
                  6,310
                  1111,203





                  share|improve this answer













                  $ column -t -s, file | sed 's/^([0-9]*)( *)/21,/'
                  120,900
                  1160,001
                  80,730
                  600,165
                  6,310
                  1111,203


                  The column output will be



                  120 900
                  1160 001
                  80 730
                  600 165
                  6 310
                  1111 203


                  and the sed command just moves the spaces between the two columns to the start of the line and inserts the comma in their place.



                  If the initial extra two spaces are not wanted, use



                  $ column -t -s, file | sed -e 's/^([0-9]*)( *)/21,/' -e 's/^ //'
                  120,900
                  1160,001
                  80,730
                  600,165
                  6,310
                  1111,203






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 5 at 15:42









                  Kusalananda

                  102k13199315




                  102k13199315






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442817%2fhow-to-left-pad-a-column-in-bash%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)