how to alignment the output of the last fields












0














the bash script



for i in $script_name
do
echo -en "running the script - $it - "
exec 3>&1 4>&2
var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures time only
exec 3>&- 4>&-

echo "$var"
done


print the following:



running the script - Verify_disk.bash -       1.42
running the script - Verify_yum_list.bash - 10.49
running the script - Verify_size.bash - 2.93
running the script - Verify_mem_size.bash - 0.71
running the script - Verify_disk_size.bash - 2.41
running the script - Verify_wdisk.bash - 1.63
running the script - Verify_cpu.bash - 0.74


since $var is the variable that print all these output
we want to alignment the output



so it will be like this



running the script - Verify_disk.bash        -   1.42
running the script - Verify_yum_list.bash - 10.49
running the script - Verify_size.bash - 2.93
running the script - Verify_mem_size.bash - 0.71
running the script - Verify_disk_size.bash - 2.41
running the script - Verify_wdisk.bash - 1.63
running the script - Verify_cpu.bash - 0.74


what are the additional changes that should be with $var in order to alignment the last fields










share|improve this question





























    0














    the bash script



    for i in $script_name
    do
    echo -en "running the script - $it - "
    exec 3>&1 4>&2
    var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures time only
    exec 3>&- 4>&-

    echo "$var"
    done


    print the following:



    running the script - Verify_disk.bash -       1.42
    running the script - Verify_yum_list.bash - 10.49
    running the script - Verify_size.bash - 2.93
    running the script - Verify_mem_size.bash - 0.71
    running the script - Verify_disk_size.bash - 2.41
    running the script - Verify_wdisk.bash - 1.63
    running the script - Verify_cpu.bash - 0.74


    since $var is the variable that print all these output
    we want to alignment the output



    so it will be like this



    running the script - Verify_disk.bash        -   1.42
    running the script - Verify_yum_list.bash - 10.49
    running the script - Verify_size.bash - 2.93
    running the script - Verify_mem_size.bash - 0.71
    running the script - Verify_disk_size.bash - 2.41
    running the script - Verify_wdisk.bash - 1.63
    running the script - Verify_cpu.bash - 0.74


    what are the additional changes that should be with $var in order to alignment the last fields










    share|improve this question



























      0












      0








      0







      the bash script



      for i in $script_name
      do
      echo -en "running the script - $it - "
      exec 3>&1 4>&2
      var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures time only
      exec 3>&- 4>&-

      echo "$var"
      done


      print the following:



      running the script - Verify_disk.bash -       1.42
      running the script - Verify_yum_list.bash - 10.49
      running the script - Verify_size.bash - 2.93
      running the script - Verify_mem_size.bash - 0.71
      running the script - Verify_disk_size.bash - 2.41
      running the script - Verify_wdisk.bash - 1.63
      running the script - Verify_cpu.bash - 0.74


      since $var is the variable that print all these output
      we want to alignment the output



      so it will be like this



      running the script - Verify_disk.bash        -   1.42
      running the script - Verify_yum_list.bash - 10.49
      running the script - Verify_size.bash - 2.93
      running the script - Verify_mem_size.bash - 0.71
      running the script - Verify_disk_size.bash - 2.41
      running the script - Verify_wdisk.bash - 1.63
      running the script - Verify_cpu.bash - 0.74


      what are the additional changes that should be with $var in order to alignment the last fields










      share|improve this question















      the bash script



      for i in $script_name
      do
      echo -en "running the script - $it - "
      exec 3>&1 4>&2
      var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures time only
      exec 3>&- 4>&-

      echo "$var"
      done


      print the following:



      running the script - Verify_disk.bash -       1.42
      running the script - Verify_yum_list.bash - 10.49
      running the script - Verify_size.bash - 2.93
      running the script - Verify_mem_size.bash - 0.71
      running the script - Verify_disk_size.bash - 2.41
      running the script - Verify_wdisk.bash - 1.63
      running the script - Verify_cpu.bash - 0.74


      since $var is the variable that print all these output
      we want to alignment the output



      so it will be like this



      running the script - Verify_disk.bash        -   1.42
      running the script - Verify_yum_list.bash - 10.49
      running the script - Verify_size.bash - 2.93
      running the script - Verify_mem_size.bash - 0.71
      running the script - Verify_disk_size.bash - 2.41
      running the script - Verify_wdisk.bash - 1.63
      running the script - Verify_cpu.bash - 0.74


      what are the additional changes that should be with $var in order to alignment the last fields







      linux bash shell-script printf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 19 '18 at 0:29

























      asked Dec 19 '18 at 0:23









      yael

      2,42612159




      2,42612159






















          2 Answers
          2






          active

          oldest

          votes


















          1














          I don't think that you can do the formatting proper inside the script as the earlier iterations of the loop have no way of knowing the width of the following variables; how about a two-step process?



          I also took the liberty (personal preference) to right-align the output of the time values.



          Having saved your example input to file (can't generate that dynamically) my suggestion looks like this:



          cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2fn",$1, $2, $3}' 
          running the script - Verify_disk.bash - 1.42
          running the script - Verify_yum_list.bash - 10.49
          running the script - Verify_size.bash - 2.93
          running the script - Verify_mem_size.bash - 0.71
          running the script - Verify_disk_size.bash - 2.41
          running the script - Verify_wdisk.bash - 1.63
          running the script - Verify_cpu.bash - 0.74


          So if you were to pipe the output of your script through my awk ...






          share|improve this answer





























            1














            Do a pre-loop to calculate the longest filename then use that as a spacing parameter:



            longest=0
            for file in *.bash
            do
            [ "${#file}" -gt "$longest" ] && longest=${#file}
            done

            # ... for your execution loop
            printf "running the script - %${longest}st- "
            printf "%sn" "$var"


            I assume that all of your scripts are covered by the wildcard *.bash; adjust that as needed. The initial loops calculates the needed width; the initial printf uses that variable to format the width of the script field for every iteration of the for loop.






            share|improve this answer





















              Your Answer








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

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

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              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%2f489790%2fhow-to-alignment-the-output-of-the-last-fields%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              I don't think that you can do the formatting proper inside the script as the earlier iterations of the loop have no way of knowing the width of the following variables; how about a two-step process?



              I also took the liberty (personal preference) to right-align the output of the time values.



              Having saved your example input to file (can't generate that dynamically) my suggestion looks like this:



              cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2fn",$1, $2, $3}' 
              running the script - Verify_disk.bash - 1.42
              running the script - Verify_yum_list.bash - 10.49
              running the script - Verify_size.bash - 2.93
              running the script - Verify_mem_size.bash - 0.71
              running the script - Verify_disk_size.bash - 2.41
              running the script - Verify_wdisk.bash - 1.63
              running the script - Verify_cpu.bash - 0.74


              So if you were to pipe the output of your script through my awk ...






              share|improve this answer


























                1














                I don't think that you can do the formatting proper inside the script as the earlier iterations of the loop have no way of knowing the width of the following variables; how about a two-step process?



                I also took the liberty (personal preference) to right-align the output of the time values.



                Having saved your example input to file (can't generate that dynamically) my suggestion looks like this:



                cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2fn",$1, $2, $3}' 
                running the script - Verify_disk.bash - 1.42
                running the script - Verify_yum_list.bash - 10.49
                running the script - Verify_size.bash - 2.93
                running the script - Verify_mem_size.bash - 0.71
                running the script - Verify_disk_size.bash - 2.41
                running the script - Verify_wdisk.bash - 1.63
                running the script - Verify_cpu.bash - 0.74


                So if you were to pipe the output of your script through my awk ...






                share|improve this answer
























                  1












                  1








                  1






                  I don't think that you can do the formatting proper inside the script as the earlier iterations of the loop have no way of knowing the width of the following variables; how about a two-step process?



                  I also took the liberty (personal preference) to right-align the output of the time values.



                  Having saved your example input to file (can't generate that dynamically) my suggestion looks like this:



                  cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2fn",$1, $2, $3}' 
                  running the script - Verify_disk.bash - 1.42
                  running the script - Verify_yum_list.bash - 10.49
                  running the script - Verify_size.bash - 2.93
                  running the script - Verify_mem_size.bash - 0.71
                  running the script - Verify_disk_size.bash - 2.41
                  running the script - Verify_wdisk.bash - 1.63
                  running the script - Verify_cpu.bash - 0.74


                  So if you were to pipe the output of your script through my awk ...






                  share|improve this answer












                  I don't think that you can do the formatting proper inside the script as the earlier iterations of the loop have no way of knowing the width of the following variables; how about a two-step process?



                  I also took the liberty (personal preference) to right-align the output of the time values.



                  Having saved your example input to file (can't generate that dynamically) my suggestion looks like this:



                  cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2fn",$1, $2, $3}' 
                  running the script - Verify_disk.bash - 1.42
                  running the script - Verify_yum_list.bash - 10.49
                  running the script - Verify_size.bash - 2.93
                  running the script - Verify_mem_size.bash - 0.71
                  running the script - Verify_disk_size.bash - 2.41
                  running the script - Verify_wdisk.bash - 1.63
                  running the script - Verify_cpu.bash - 0.74


                  So if you were to pipe the output of your script through my awk ...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '18 at 0:50









                  tink

                  4,16311219




                  4,16311219

























                      1














                      Do a pre-loop to calculate the longest filename then use that as a spacing parameter:



                      longest=0
                      for file in *.bash
                      do
                      [ "${#file}" -gt "$longest" ] && longest=${#file}
                      done

                      # ... for your execution loop
                      printf "running the script - %${longest}st- "
                      printf "%sn" "$var"


                      I assume that all of your scripts are covered by the wildcard *.bash; adjust that as needed. The initial loops calculates the needed width; the initial printf uses that variable to format the width of the script field for every iteration of the for loop.






                      share|improve this answer


























                        1














                        Do a pre-loop to calculate the longest filename then use that as a spacing parameter:



                        longest=0
                        for file in *.bash
                        do
                        [ "${#file}" -gt "$longest" ] && longest=${#file}
                        done

                        # ... for your execution loop
                        printf "running the script - %${longest}st- "
                        printf "%sn" "$var"


                        I assume that all of your scripts are covered by the wildcard *.bash; adjust that as needed. The initial loops calculates the needed width; the initial printf uses that variable to format the width of the script field for every iteration of the for loop.






                        share|improve this answer
























                          1












                          1








                          1






                          Do a pre-loop to calculate the longest filename then use that as a spacing parameter:



                          longest=0
                          for file in *.bash
                          do
                          [ "${#file}" -gt "$longest" ] && longest=${#file}
                          done

                          # ... for your execution loop
                          printf "running the script - %${longest}st- "
                          printf "%sn" "$var"


                          I assume that all of your scripts are covered by the wildcard *.bash; adjust that as needed. The initial loops calculates the needed width; the initial printf uses that variable to format the width of the script field for every iteration of the for loop.






                          share|improve this answer












                          Do a pre-loop to calculate the longest filename then use that as a spacing parameter:



                          longest=0
                          for file in *.bash
                          do
                          [ "${#file}" -gt "$longest" ] && longest=${#file}
                          done

                          # ... for your execution loop
                          printf "running the script - %${longest}st- "
                          printf "%sn" "$var"


                          I assume that all of your scripts are covered by the wildcard *.bash; adjust that as needed. The initial loops calculates the needed width; the initial printf uses that variable to format the width of the script field for every iteration of the for loop.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 19 '18 at 2:07









                          Jeff Schaller

                          38.7k1053125




                          38.7k1053125






























                              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%2f489790%2fhow-to-alignment-the-output-of-the-last-fields%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