Align columns evenly without “column - t”











up vote
1
down vote

favorite
1












I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t is not available on AIX.



Actual File:



     X1vir1000      x1-DFB127   x1ttcb101_mv_03_2016   Not-activated  16         x1ttcr181   x1ttcr182
X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013


Expected Result:


X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013









share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t is not available on AIX.



    Actual File:



         X1vir1000      x1-DFB127   x1ttcb101_mv_03_2016   Not-activated  16         x1ttcr181   x1ttcr182
    X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
    X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
    X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
    X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
    X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
    X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
    X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
    X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
    X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013


    Expected Result:


    X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
    X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
    X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
    X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
    X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
    X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
    X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
    X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
    X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
    X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013









    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t is not available on AIX.



      Actual File:



           X1vir1000      x1-DFB127   x1ttcb101_mv_03_2016   Not-activated  16         x1ttcr181   x1ttcr182
      X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
      X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
      X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
      X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
      X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
      X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013


      Expected Result:


      X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
      X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
      X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
      X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
      X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
      X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
      X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013









      share|improve this question















      I have an output file created from a Korn shell script. I need to align the output with spaces evenly. Unfortunately column -t is not available on AIX.



      Actual File:



           X1vir1000      x1-DFB127   x1ttcb101_mv_03_2016   Not-activated  16         x1ttcr181   x1ttcr182
      X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
      X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
      X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
      X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
      X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
      X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013


      Expected Result:


      X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
      X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
      X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
      X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
      X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
      X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
      X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
      X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013






      shell-script awk sed ksh aix






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 at 7:53









      Kusalananda

      118k16223364




      118k16223364










      asked Nov 27 at 8:12









      satsensort

      248




      248






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }


          This awk script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.



          If the delim variable is set, it is used to delimit the columns, otherwise a space character is used.



          The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't' on the command line.



          Two test runs on the given data (note that the input file has to be specified twice):



          $ awk -f ./script.awk file file
          X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
          X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
          X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
          X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
          X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
          X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
          X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013




          $ awk -v delim=' | ' -f ./script.awk file file
          X1vir1000 | x1-DFB127 | x1ttcb101_mv_03_2016 | Not-activated | 16 | x1ttcr181 | x1ttcr182
          X1vir1000 | x1-DFB127 | x1dvdb201_mv_pmp-132 | Not-activated | 3 | x1ttcr181 | x1ttcr182
          X3vir1000 | x3-DFB116 | x3dvdb202_mv_05032016 | Not-activated | 22 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3ttcb203_mv_03_2016 | Not-activated | 19 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3trcb223_mv_10_2017 | Not-activated | 29 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB117 | x3trvf245_mv_08_2017 | Not-activated | 27 | x3ttcr177 | x3ttcr178
          X3vir1000 | x3-DFB131 | CR74536_x3dvap234_Decom | Not-activated | 101 | x3ttcr213 | x3ttcr214
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
          X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm | Not-activated | 4 | x9ttcr012 | x9ttcr013




          A shell script that embeds the above awk program and takes two options:





          • -d delim where delim is the output delimiter to use.


          • -D delim where delim is the input delimiter to use (e.g. -D 't' for tabs in the input data).


          The script would be use like this to recreate the two runs above:



          ./script.sh file
          ./script.sh -d ' | ' file


          The script:



          #!/bin/sh

          while getopts 'd:D:' opt; do
          case $opt in
          d) delim=$OPTARG ;;
          D) fs=$OPTARG ;;
          *) echo 'Error in command line parsing' >&2
          exit 1
          esac
          done
          shift "$(( OPTIND - 1 ))"

          tmpfile=$(mktemp)
          # If mktemp is not available:
          # tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
          # ... or something similar
          cat "$1" >$tmpfile

          awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }' "$tmpfile" "$tmpfile"

          rm -f "$tmpfile"





          share|improve this answer























          • Many thanks @kusalananda. The awk script helped in aligning the text
            – satsensort
            Nov 27 at 9:55






          • 1




            If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
            – glenn jackman
            Nov 27 at 15:59










          • @glennjackman Thanks, didn't spot that when I wrote it.
            – Kusalananda
            Nov 29 at 7:48











          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%2f484390%2falign-columns-evenly-without-column-t%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }


          This awk script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.



          If the delim variable is set, it is used to delimit the columns, otherwise a space character is used.



          The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't' on the command line.



          Two test runs on the given data (note that the input file has to be specified twice):



          $ awk -f ./script.awk file file
          X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
          X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
          X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
          X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
          X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
          X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
          X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013




          $ awk -v delim=' | ' -f ./script.awk file file
          X1vir1000 | x1-DFB127 | x1ttcb101_mv_03_2016 | Not-activated | 16 | x1ttcr181 | x1ttcr182
          X1vir1000 | x1-DFB127 | x1dvdb201_mv_pmp-132 | Not-activated | 3 | x1ttcr181 | x1ttcr182
          X3vir1000 | x3-DFB116 | x3dvdb202_mv_05032016 | Not-activated | 22 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3ttcb203_mv_03_2016 | Not-activated | 19 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3trcb223_mv_10_2017 | Not-activated | 29 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB117 | x3trvf245_mv_08_2017 | Not-activated | 27 | x3ttcr177 | x3ttcr178
          X3vir1000 | x3-DFB131 | CR74536_x3dvap234_Decom | Not-activated | 101 | x3ttcr213 | x3ttcr214
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
          X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm | Not-activated | 4 | x9ttcr012 | x9ttcr013




          A shell script that embeds the above awk program and takes two options:





          • -d delim where delim is the output delimiter to use.


          • -D delim where delim is the input delimiter to use (e.g. -D 't' for tabs in the input data).


          The script would be use like this to recreate the two runs above:



          ./script.sh file
          ./script.sh -d ' | ' file


          The script:



          #!/bin/sh

          while getopts 'd:D:' opt; do
          case $opt in
          d) delim=$OPTARG ;;
          D) fs=$OPTARG ;;
          *) echo 'Error in command line parsing' >&2
          exit 1
          esac
          done
          shift "$(( OPTIND - 1 ))"

          tmpfile=$(mktemp)
          # If mktemp is not available:
          # tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
          # ... or something similar
          cat "$1" >$tmpfile

          awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }' "$tmpfile" "$tmpfile"

          rm -f "$tmpfile"





          share|improve this answer























          • Many thanks @kusalananda. The awk script helped in aligning the text
            – satsensort
            Nov 27 at 9:55






          • 1




            If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
            – glenn jackman
            Nov 27 at 15:59










          • @glennjackman Thanks, didn't spot that when I wrote it.
            – Kusalananda
            Nov 29 at 7:48















          up vote
          2
          down vote



          accepted










          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }


          This awk script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.



          If the delim variable is set, it is used to delimit the columns, otherwise a space character is used.



          The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't' on the command line.



          Two test runs on the given data (note that the input file has to be specified twice):



          $ awk -f ./script.awk file file
          X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
          X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
          X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
          X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
          X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
          X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
          X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013




          $ awk -v delim=' | ' -f ./script.awk file file
          X1vir1000 | x1-DFB127 | x1ttcb101_mv_03_2016 | Not-activated | 16 | x1ttcr181 | x1ttcr182
          X1vir1000 | x1-DFB127 | x1dvdb201_mv_pmp-132 | Not-activated | 3 | x1ttcr181 | x1ttcr182
          X3vir1000 | x3-DFB116 | x3dvdb202_mv_05032016 | Not-activated | 22 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3ttcb203_mv_03_2016 | Not-activated | 19 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3trcb223_mv_10_2017 | Not-activated | 29 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB117 | x3trvf245_mv_08_2017 | Not-activated | 27 | x3ttcr177 | x3ttcr178
          X3vir1000 | x3-DFB131 | CR74536_x3dvap234_Decom | Not-activated | 101 | x3ttcr213 | x3ttcr214
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
          X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm | Not-activated | 4 | x9ttcr012 | x9ttcr013




          A shell script that embeds the above awk program and takes two options:





          • -d delim where delim is the output delimiter to use.


          • -D delim where delim is the input delimiter to use (e.g. -D 't' for tabs in the input data).


          The script would be use like this to recreate the two runs above:



          ./script.sh file
          ./script.sh -d ' | ' file


          The script:



          #!/bin/sh

          while getopts 'd:D:' opt; do
          case $opt in
          d) delim=$OPTARG ;;
          D) fs=$OPTARG ;;
          *) echo 'Error in command line parsing' >&2
          exit 1
          esac
          done
          shift "$(( OPTIND - 1 ))"

          tmpfile=$(mktemp)
          # If mktemp is not available:
          # tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
          # ... or something similar
          cat "$1" >$tmpfile

          awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }' "$tmpfile" "$tmpfile"

          rm -f "$tmpfile"





          share|improve this answer























          • Many thanks @kusalananda. The awk script helped in aligning the text
            – satsensort
            Nov 27 at 9:55






          • 1




            If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
            – glenn jackman
            Nov 27 at 15:59










          • @glennjackman Thanks, didn't spot that when I wrote it.
            – Kusalananda
            Nov 29 at 7:48













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }


          This awk script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.



          If the delim variable is set, it is used to delimit the columns, otherwise a space character is used.



          The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't' on the command line.



          Two test runs on the given data (note that the input file has to be specified twice):



          $ awk -f ./script.awk file file
          X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
          X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
          X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
          X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
          X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
          X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
          X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013




          $ awk -v delim=' | ' -f ./script.awk file file
          X1vir1000 | x1-DFB127 | x1ttcb101_mv_03_2016 | Not-activated | 16 | x1ttcr181 | x1ttcr182
          X1vir1000 | x1-DFB127 | x1dvdb201_mv_pmp-132 | Not-activated | 3 | x1ttcr181 | x1ttcr182
          X3vir1000 | x3-DFB116 | x3dvdb202_mv_05032016 | Not-activated | 22 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3ttcb203_mv_03_2016 | Not-activated | 19 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3trcb223_mv_10_2017 | Not-activated | 29 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB117 | x3trvf245_mv_08_2017 | Not-activated | 27 | x3ttcr177 | x3ttcr178
          X3vir1000 | x3-DFB131 | CR74536_x3dvap234_Decom | Not-activated | 101 | x3ttcr213 | x3ttcr214
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
          X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm | Not-activated | 4 | x9ttcr012 | x9ttcr013




          A shell script that embeds the above awk program and takes two options:





          • -d delim where delim is the output delimiter to use.


          • -D delim where delim is the input delimiter to use (e.g. -D 't' for tabs in the input data).


          The script would be use like this to recreate the two runs above:



          ./script.sh file
          ./script.sh -d ' | ' file


          The script:



          #!/bin/sh

          while getopts 'd:D:' opt; do
          case $opt in
          d) delim=$OPTARG ;;
          D) fs=$OPTARG ;;
          *) echo 'Error in command line parsing' >&2
          exit 1
          esac
          done
          shift "$(( OPTIND - 1 ))"

          tmpfile=$(mktemp)
          # If mktemp is not available:
          # tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
          # ... or something similar
          cat "$1" >$tmpfile

          awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }' "$tmpfile" "$tmpfile"

          rm -f "$tmpfile"





          share|improve this answer














          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }


          This awk script expects to read the same file twice. The first time, it records the maximum width of each column in the input data. The second time, it prints the columns formatted to that maximum width.



          If the delim variable is set, it is used to delimit the columns, otherwise a space character is used.



          The default is to assume that the original data is whitespace-delimited. If it is tab-delimited, use -F 't' on the command line.



          Two test runs on the given data (note that the input file has to be specified twice):



          $ awk -f ./script.awk file file
          X1vir1000 x1-DFB127 x1ttcb101_mv_03_2016 Not-activated 16 x1ttcr181 x1ttcr182
          X1vir1000 x1-DFB127 x1dvdb201_mv_pmp-132 Not-activated 3 x1ttcr181 x1ttcr182
          X3vir1000 x3-DFB116 x3dvdb202_mv_05032016 Not-activated 22 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3ttcb203_mv_03_2016 Not-activated 19 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB116 x3trcb223_mv_10_2017 Not-activated 29 x3ttcr175 x3ttcr176
          X3vir1000 x3-DFB117 x3trvf245_mv_08_2017 Not-activated 27 x3ttcr177 x3ttcr178
          X3vir1000 x3-DFB131 CR74536_x3dvap234_Decom Not-activated 101 x3ttcr213 x3ttcr214
          X3vir1000 x3-DFB132 decommissioned_x3trcb223 Not-activated 138 x3ttcr217 x3ttcr218
          X3vir1000 x3-DFB132 decommissioned_x3trcb312 Not-activated 116 x3ttcr217 x3ttcr218
          X9vir1000 x9-DFB005-8233-E8B-SN1030BDR x9trcb003_vhost1_decomm Not-activated 4 x9ttcr012 x9ttcr013




          $ awk -v delim=' | ' -f ./script.awk file file
          X1vir1000 | x1-DFB127 | x1ttcb101_mv_03_2016 | Not-activated | 16 | x1ttcr181 | x1ttcr182
          X1vir1000 | x1-DFB127 | x1dvdb201_mv_pmp-132 | Not-activated | 3 | x1ttcr181 | x1ttcr182
          X3vir1000 | x3-DFB116 | x3dvdb202_mv_05032016 | Not-activated | 22 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3ttcb203_mv_03_2016 | Not-activated | 19 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB116 | x3trcb223_mv_10_2017 | Not-activated | 29 | x3ttcr175 | x3ttcr176
          X3vir1000 | x3-DFB117 | x3trvf245_mv_08_2017 | Not-activated | 27 | x3ttcr177 | x3ttcr178
          X3vir1000 | x3-DFB131 | CR74536_x3dvap234_Decom | Not-activated | 101 | x3ttcr213 | x3ttcr214
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb223 | Not-activated | 138 | x3ttcr217 | x3ttcr218
          X3vir1000 | x3-DFB132 | decommissioned_x3trcb312 | Not-activated | 116 | x3ttcr217 | x3ttcr218
          X9vir1000 | x9-DFB005-8233-E8B-SN1030BDR | x9trcb003_vhost1_decomm | Not-activated | 4 | x9ttcr012 | x9ttcr013




          A shell script that embeds the above awk program and takes two options:





          • -d delim where delim is the output delimiter to use.


          • -D delim where delim is the input delimiter to use (e.g. -D 't' for tabs in the input data).


          The script would be use like this to recreate the two runs above:



          ./script.sh file
          ./script.sh -d ' | ' file


          The script:



          #!/bin/sh

          while getopts 'd:D:' opt; do
          case $opt in
          d) delim=$OPTARG ;;
          D) fs=$OPTARG ;;
          *) echo 'Error in command line parsing' >&2
          exit 1
          esac
          done
          shift "$(( OPTIND - 1 ))"

          tmpfile=$(mktemp)
          # If mktemp is not available:
          # tmpfile="${TMPDIR:-/tmp}/columnn-t.tmp"
          # ... or something similar
          cat "$1" >$tmpfile

          awk ${delim:+-v delim="$delim"} ${fs:+-F "$fs"} '
          FNR == NR {
          for (i = 1; i <= NF; ++i) {
          width = length($i)
          maxwidth[i] = (width > maxwidth[i] ? width : maxwidth[i])
          }

          next
          }

          {
          for (i = 1; i <= NF; ++i)
          printf("%-*s%s", maxwidth[i], $i,
          (i == NF ? "n" : (delim == "" ? " " : delim)) )
          }' "$tmpfile" "$tmpfile"

          rm -f "$tmpfile"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 at 7:58

























          answered Nov 27 at 9:06









          Kusalananda

          118k16223364




          118k16223364












          • Many thanks @kusalananda. The awk script helped in aligning the text
            – satsensort
            Nov 27 at 9:55






          • 1




            If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
            – glenn jackman
            Nov 27 at 15:59










          • @glennjackman Thanks, didn't spot that when I wrote it.
            – Kusalananda
            Nov 29 at 7:48


















          • Many thanks @kusalananda. The awk script helped in aligning the text
            – satsensort
            Nov 27 at 9:55






          • 1




            If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
            – glenn jackman
            Nov 27 at 15:59










          • @glennjackman Thanks, didn't spot that when I wrote it.
            – Kusalananda
            Nov 29 at 7:48
















          Many thanks @kusalananda. The awk script helped in aligning the text
          – satsensort
          Nov 27 at 9:55




          Many thanks @kusalananda. The awk script helped in aligning the text
          – satsensort
          Nov 27 at 9:55




          1




          1




          If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
          – glenn jackman
          Nov 27 at 15:59




          If i == NF, you can use ORS instead of "", and thus remove explicitly printing a newline.
          – glenn jackman
          Nov 27 at 15:59












          @glennjackman Thanks, didn't spot that when I wrote it.
          – Kusalananda
          Nov 29 at 7:48




          @glennjackman Thanks, didn't spot that when I wrote it.
          – Kusalananda
          Nov 29 at 7:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484390%2falign-columns-evenly-without-column-t%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre