Calling an array inside awk to create a table with fixed-width columns

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











up vote
5
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 anyway 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
    5
    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 anyway 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
      5
      down vote

      favorite









      up vote
      5
      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 anyway 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 anyway 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 55 mins ago

























      asked 3 hours ago









      Goro

      2,55141849




      2,55141849




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          8
          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
            1 hour ago






          • 1




            Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
            – Stephen Harris
            1 hour ago

















          up vote
          2
          down vote













          You could (but I do not recommend 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).



          Or even:



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


          You may change the format to %-5.5s to cut fields that are longer than 5 characters.



          Note that awk's printf incorrectly counts decomposed character like é as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.




          EDIT
          Answering this additional question from the comments:




          sum the rows that define column #27 in my table




          Just add the needed code:



          $ awk 'split($0,a); 
          sum=sum+a[27];
          for(i in a) printf "%-5s", $i ;
          print ""
          END
          print "Sum of column 27 is =", sum
          ' infile





          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
            1 hour ago











          • I wish I can give you two thumb-up ;-) thanks again!
            – Goro
            1 hour ago











          • Hey, ... You could still select this answer as the accepted one ! :-)
            – Isaac
            1 hour 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-a-table-with-fixed-width-columns%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          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
            1 hour ago






          • 1




            Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
            – Stephen Harris
            1 hour ago














          up vote
          8
          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
            1 hour ago






          • 1




            Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
            – Stephen Harris
            1 hour ago












          up vote
          8
          down vote



          accepted







          up vote
          8
          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 3 hours ago









          Stephen Harris

          21.6k23973




          21.6k23973











          • 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
            1 hour ago






          • 1




            Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
            – Stephen Harris
            1 hour 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
            1 hour ago






          • 1




            Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
            – Stephen Harris
            1 hour 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
          1 hour 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
          1 hour ago




          1




          1




          Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
          – Stephen Harris
          1 hour ago




          Before the for loop you could do something like sum += $27 and then at the very end you can use something like END print sum to print it out
          – Stephen Harris
          1 hour ago












          up vote
          2
          down vote













          You could (but I do not recommend 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).



          Or even:



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


          You may change the format to %-5.5s to cut fields that are longer than 5 characters.



          Note that awk's printf incorrectly counts decomposed character like é as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.




          EDIT
          Answering this additional question from the comments:




          sum the rows that define column #27 in my table




          Just add the needed code:



          $ awk 'split($0,a); 
          sum=sum+a[27];
          for(i in a) printf "%-5s", $i ;
          print ""
          END
          print "Sum of column 27 is =", sum
          ' infile





          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
            1 hour ago











          • I wish I can give you two thumb-up ;-) thanks again!
            – Goro
            1 hour ago











          • Hey, ... You could still select this answer as the accepted one ! :-)
            – Isaac
            1 hour ago














          up vote
          2
          down vote













          You could (but I do not recommend 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).



          Or even:



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


          You may change the format to %-5.5s to cut fields that are longer than 5 characters.



          Note that awk's printf incorrectly counts decomposed character like é as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.




          EDIT
          Answering this additional question from the comments:




          sum the rows that define column #27 in my table




          Just add the needed code:



          $ awk 'split($0,a); 
          sum=sum+a[27];
          for(i in a) printf "%-5s", $i ;
          print ""
          END
          print "Sum of column 27 is =", sum
          ' infile





          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
            1 hour ago











          • I wish I can give you two thumb-up ;-) thanks again!
            – Goro
            1 hour ago











          • Hey, ... You could still select this answer as the accepted one ! :-)
            – Isaac
            1 hour ago












          up vote
          2
          down vote










          up vote
          2
          down vote









          You could (but I do not recommend 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).



          Or even:



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


          You may change the format to %-5.5s to cut fields that are longer than 5 characters.



          Note that awk's printf incorrectly counts decomposed character like é as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.




          EDIT
          Answering this additional question from the comments:




          sum the rows that define column #27 in my table




          Just add the needed code:



          $ awk 'split($0,a); 
          sum=sum+a[27];
          for(i in a) printf "%-5s", $i ;
          print ""
          END
          print "Sum of column 27 is =", sum
          ' infile





          share|improve this answer














          You could (but I do not recommend 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).



          Or even:



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


          You may change the format to %-5.5s to cut fields that are longer than 5 characters.



          Note that awk's printf incorrectly counts decomposed character like é as two characters. It seems to count Unicode code points (a common issue) instead of Unicode clusters.




          EDIT
          Answering this additional question from the comments:




          sum the rows that define column #27 in my table




          Just add the needed code:



          $ awk 'split($0,a); 
          sum=sum+a[27];
          for(i in a) printf "%-5s", $i ;
          print ""
          END
          print "Sum of column 27 is =", sum
          ' infile






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Isaac

          7,0721834




          7,0721834











          • 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
            1 hour ago











          • I wish I can give you two thumb-up ;-) thanks again!
            – Goro
            1 hour ago











          • Hey, ... You could still select this answer as the accepted one ! :-)
            – Isaac
            1 hour 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
            1 hour ago











          • I wish I can give you two thumb-up ;-) thanks again!
            – Goro
            1 hour ago











          • Hey, ... You could still select this answer as the accepted one ! :-)
            – Isaac
            1 hour 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
          1 hour 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
          1 hour ago













          I wish I can give you two thumb-up ;-) thanks again!
          – Goro
          1 hour ago





          I wish I can give you two thumb-up ;-) thanks again!
          – Goro
          1 hour ago













          Hey, ... You could still select this answer as the accepted one ! :-)
          – Isaac
          1 hour ago




          Hey, ... You could still select this answer as the accepted one ! :-)
          – Isaac
          1 hour 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-a-table-with-fixed-width-columns%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          What does second last employer means? [closed]

          One-line joke