Calling an array inside awk to create fixed-widths columns table

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











up vote
3
down vote

favorite












I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:



awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'


Is there away to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:



awk 'BEGIN for i in 1..30; do echo %-5sn print i


How can I implement that correctly inside awk to create multiple fixed-widths columns?










share|improve this question



























    up vote
    3
    down vote

    favorite












    I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:



    awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'


    Is there away to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:



    awk 'BEGIN for i in 1..30; do echo %-5sn print i


    How can I implement that correctly inside awk to create multiple fixed-widths columns?










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:



      awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'


      Is there away to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:



      awk 'BEGIN for i in 1..30; do echo %-5sn print i


      How can I implement that correctly inside awk to create multiple fixed-widths columns?










      share|improve this question















      I would like to extract data from a file and organize it in a big fixed-widths table. I can expect that this table will have multiple columns, let's say 30 columns. If I create this table using the traditional awk command line, then I will need to write a very long awk command line, something similar to the following:



      awk 'printf "%-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5sn", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30'


      Is there away to make this linear shorter? For example, I am thinking of implementing an array inside the previous long command. This array will tell awk what are the numbers and the widths of the columns that I would like to create, instead of defining each column separately, something like:



      awk 'BEGIN for i in 1..30; do echo %-5sn print i


      How can I implement that correctly inside awk to create multiple fixed-widths columns?







      awk columns






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 2 hours ago









      Goro

      2,50141849




      2,50141849




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You can do the print, itself, inside a loop, one field at a time.



          awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '


          Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.



          e.g



          echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
          a b c 32 87 x5





          share|improve this answer




















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago

















          up vote
          1
          down vote













          Another option is using awk's system() function and call printf:



          awk ' system("printf %-5s " $0); print "" ' infile
          a b c 32 87 x5


          This behaves like this that we pass the whole line to the shell printf command as like unquoted string, so that %-5s will apply for every arguments seen.



          compare:



          printf '%-5s' '1 2 3'
          1 2 3


          with



          printf '%-5s' 1 2 3
          1 2 3





          share|improve this answer






















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago










          • Note that there is the risk that a field will get expanded if it is a glob (like *).
            – Isaac
            1 min ago

















          up vote
          1
          down vote













          You could (but I do not recomend this) build some vars in steps (an example in bash):



          $ printf -v l '%s ' 1..30 # list of numbers to use
          $ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
          $ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
          $ awk -va="$a" 'printf a "n"'"$b"'' infile4


          But you could also do it all inside awk:



          $ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile


          • The split in awk will use the same regex in FS as used to split the line into fields and place each value in the array a.

          • The for will (auto) loop over all fields.

          • The printf will print all fields with the same format.

          • And, the final print will place a newline at the end of the line.

          This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).





          share




















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            3 mins ago











          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%2f469550%2fcalling-an-array-inside-awk-to-create-fixed-widths-columns-table%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
          6
          down vote



          accepted










          You can do the print, itself, inside a loop, one field at a time.



          awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '


          Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.



          e.g



          echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
          a b c 32 87 x5





          share|improve this answer




















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago














          up vote
          6
          down vote



          accepted










          You can do the print, itself, inside a loop, one field at a time.



          awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '


          Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.



          e.g



          echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
          a b c 32 87 x5





          share|improve this answer




















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          You can do the print, itself, inside a loop, one field at a time.



          awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '


          Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.



          e.g



          echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
          a b c 32 87 x5





          share|improve this answer












          You can do the print, itself, inside a loop, one field at a time.



          awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '


          Note the printing of the newline is needed after the loop to prevent multiple lines all merging into one.



          e.g



          echo a b c 32 87 x5 | awk 'for(i=1;i<=NF;i++) printf "%-5s",$i ; printf("n"); '
          a b c 32 87 x5






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Stephen Harris

          21.5k23973




          21.5k23973











          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago
















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago















          Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
          – Goro
          2 mins ago




          Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
          – Goro
          2 mins ago












          up vote
          1
          down vote













          Another option is using awk's system() function and call printf:



          awk ' system("printf %-5s " $0); print "" ' infile
          a b c 32 87 x5


          This behaves like this that we pass the whole line to the shell printf command as like unquoted string, so that %-5s will apply for every arguments seen.



          compare:



          printf '%-5s' '1 2 3'
          1 2 3


          with



          printf '%-5s' 1 2 3
          1 2 3





          share|improve this answer






















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago










          • Note that there is the risk that a field will get expanded if it is a glob (like *).
            – Isaac
            1 min ago














          up vote
          1
          down vote













          Another option is using awk's system() function and call printf:



          awk ' system("printf %-5s " $0); print "" ' infile
          a b c 32 87 x5


          This behaves like this that we pass the whole line to the shell printf command as like unquoted string, so that %-5s will apply for every arguments seen.



          compare:



          printf '%-5s' '1 2 3'
          1 2 3


          with



          printf '%-5s' 1 2 3
          1 2 3





          share|improve this answer






















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago










          • Note that there is the risk that a field will get expanded if it is a glob (like *).
            – Isaac
            1 min ago












          up vote
          1
          down vote










          up vote
          1
          down vote









          Another option is using awk's system() function and call printf:



          awk ' system("printf %-5s " $0); print "" ' infile
          a b c 32 87 x5


          This behaves like this that we pass the whole line to the shell printf command as like unquoted string, so that %-5s will apply for every arguments seen.



          compare:



          printf '%-5s' '1 2 3'
          1 2 3


          with



          printf '%-5s' 1 2 3
          1 2 3





          share|improve this answer














          Another option is using awk's system() function and call printf:



          awk ' system("printf %-5s " $0); print "" ' infile
          a b c 32 87 x5


          This behaves like this that we pass the whole line to the shell printf command as like unquoted string, so that %-5s will apply for every arguments seen.



          compare:



          printf '%-5s' '1 2 3'
          1 2 3


          with



          printf '%-5s' 1 2 3
          1 2 3






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 mins ago

























          answered 16 mins ago









          αғsнιη

          15.7k92563




          15.7k92563











          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago










          • Note that there is the risk that a field will get expanded if it is a glob (like *).
            – Isaac
            1 min ago
















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            2 mins ago










          • Note that there is the risk that a field will get expanded if it is a glob (like *).
            – Isaac
            1 min ago















          Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
          – Goro
          2 mins ago




          Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
          – Goro
          2 mins ago












          Note that there is the risk that a field will get expanded if it is a glob (like *).
          – Isaac
          1 min ago




          Note that there is the risk that a field will get expanded if it is a glob (like *).
          – Isaac
          1 min ago










          up vote
          1
          down vote













          You could (but I do not recomend this) build some vars in steps (an example in bash):



          $ printf -v l '%s ' 1..30 # list of numbers to use
          $ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
          $ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
          $ awk -va="$a" 'printf a "n"'"$b"'' infile4


          But you could also do it all inside awk:



          $ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile


          • The split in awk will use the same regex in FS as used to split the line into fields and place each value in the array a.

          • The for will (auto) loop over all fields.

          • The printf will print all fields with the same format.

          • And, the final print will place a newline at the end of the line.

          This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).





          share




















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            3 mins ago















          up vote
          1
          down vote













          You could (but I do not recomend this) build some vars in steps (an example in bash):



          $ printf -v l '%s ' 1..30 # list of numbers to use
          $ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
          $ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
          $ awk -va="$a" 'printf a "n"'"$b"'' infile4


          But you could also do it all inside awk:



          $ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile


          • The split in awk will use the same regex in FS as used to split the line into fields and place each value in the array a.

          • The for will (auto) loop over all fields.

          • The printf will print all fields with the same format.

          • And, the final print will place a newline at the end of the line.

          This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).





          share




















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            3 mins ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          You could (but I do not recomend this) build some vars in steps (an example in bash):



          $ printf -v l '%s ' 1..30 # list of numbers to use
          $ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
          $ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
          $ awk -va="$a" 'printf a "n"'"$b"'' infile4


          But you could also do it all inside awk:



          $ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile


          • The split in awk will use the same regex in FS as used to split the line into fields and place each value in the array a.

          • The for will (auto) loop over all fields.

          • The printf will print all fields with the same format.

          • And, the final print will place a newline at the end of the line.

          This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).





          share












          You could (but I do not recomend this) build some vars in steps (an example in bash):



          $ printf -v l '%s ' 1..30 # list of numbers to use
          $ printf -v a '%.0s%%-5s ' $l # make a string of repeated "%-5s"
          $ printf -v b ',$%s' $l # make string of field numbers as "$1,$2.."
          $ awk -va="$a" 'printf a "n"'"$b"'' infile4


          But you could also do it all inside awk:



          $ awk 'split($0,a); for(i in a)printf "%-5s", $i; print ""' infile


          • The split in awk will use the same regex in FS as used to split the line into fields and place each value in the array a.

          • The for will (auto) loop over all fields.

          • The printf will print all fields with the same format.

          • And, the final print will place a newline at the end of the line.

          This is more flexible as it will work for any amount of fields, even lines with varying number of fields. And is complete inside only one language (easier to understand and maintain).






          share











          share


          share










          answered 6 mins ago









          Isaac

          7,0621834




          7,0621834











          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            3 mins ago

















          • Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
            – Goro
            3 mins ago
















          Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
          – Goro
          3 mins ago





          Thank you for the solution. I would like to kindly inquire about operations in specific columns. Let's say, I would like to sum the rows that define column #27 in my table. How can I include this operation in your command? Thanks again!
          – Goro
          3 mins ago


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f469550%2fcalling-an-array-inside-awk-to-create-fixed-widths-columns-table%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery