Average of multiple numbers - Shell script












0














I have created a script which views a logfile and greps for a specific amount of milliseconds / seconds. I have created something which views the minimum and maximum value however I need to find the average of the list.



logdir_8080='/opt/product/apachetomcat/8.5.5_8080/logs'
cd "$logdir_8080"

get_file=`ls -rt localhost_access_log.*.txt|tail -1`

cat $get_file | tail -1000 | grep "objectId" | awk -F 'HTTP/1.1" 200' '{
print $2}'|awk -F' ' '{ print $2 }' | sort - n>/opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt


min_value:



cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|head -1


max_value:



cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|tail -1


Idea of the list:



233
249
283
283
302
303
332
333
643
851
965
965
972
1022
1135
1182
1213
1232
1264
1273
1390
1403
1414
1429
1474
1537
1540
1543
1545
1556
1565
1566
1577
1589
1591
1599
1602
1621
1622
1647
1653
1705
1740
1772
1774
1933
1935
1983
1990


How to get the average?










share|improve this question





























    0














    I have created a script which views a logfile and greps for a specific amount of milliseconds / seconds. I have created something which views the minimum and maximum value however I need to find the average of the list.



    logdir_8080='/opt/product/apachetomcat/8.5.5_8080/logs'
    cd "$logdir_8080"

    get_file=`ls -rt localhost_access_log.*.txt|tail -1`

    cat $get_file | tail -1000 | grep "objectId" | awk -F 'HTTP/1.1" 200' '{
    print $2}'|awk -F' ' '{ print $2 }' | sort - n>/opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt


    min_value:



    cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|head -1


    max_value:



    cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|tail -1


    Idea of the list:



    233
    249
    283
    283
    302
    303
    332
    333
    643
    851
    965
    965
    972
    1022
    1135
    1182
    1213
    1232
    1264
    1273
    1390
    1403
    1414
    1429
    1474
    1537
    1540
    1543
    1545
    1556
    1565
    1566
    1577
    1589
    1591
    1599
    1602
    1621
    1622
    1647
    1653
    1705
    1740
    1772
    1774
    1933
    1935
    1983
    1990


    How to get the average?










    share|improve this question



























      0












      0








      0







      I have created a script which views a logfile and greps for a specific amount of milliseconds / seconds. I have created something which views the minimum and maximum value however I need to find the average of the list.



      logdir_8080='/opt/product/apachetomcat/8.5.5_8080/logs'
      cd "$logdir_8080"

      get_file=`ls -rt localhost_access_log.*.txt|tail -1`

      cat $get_file | tail -1000 | grep "objectId" | awk -F 'HTTP/1.1" 200' '{
      print $2}'|awk -F' ' '{ print $2 }' | sort - n>/opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt


      min_value:



      cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|head -1


      max_value:



      cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|tail -1


      Idea of the list:



      233
      249
      283
      283
      302
      303
      332
      333
      643
      851
      965
      965
      972
      1022
      1135
      1182
      1213
      1232
      1264
      1273
      1390
      1403
      1414
      1429
      1474
      1537
      1540
      1543
      1545
      1556
      1565
      1566
      1577
      1589
      1591
      1599
      1602
      1621
      1622
      1647
      1653
      1705
      1740
      1772
      1774
      1933
      1935
      1983
      1990


      How to get the average?










      share|improve this question















      I have created a script which views a logfile and greps for a specific amount of milliseconds / seconds. I have created something which views the minimum and maximum value however I need to find the average of the list.



      logdir_8080='/opt/product/apachetomcat/8.5.5_8080/logs'
      cd "$logdir_8080"

      get_file=`ls -rt localhost_access_log.*.txt|tail -1`

      cat $get_file | tail -1000 | grep "objectId" | awk -F 'HTTP/1.1" 200' '{
      print $2}'|awk -F' ' '{ print $2 }' | sort - n>/opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt


      min_value:



      cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|head -1


      max_value:



      cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/8080.txt|tail -1


      Idea of the list:



      233
      249
      283
      283
      302
      303
      332
      333
      643
      851
      965
      965
      972
      1022
      1135
      1182
      1213
      1232
      1264
      1273
      1390
      1403
      1414
      1429
      1474
      1537
      1540
      1543
      1545
      1556
      1565
      1566
      1577
      1589
      1591
      1599
      1602
      1621
      1622
      1647
      1653
      1705
      1740
      1772
      1774
      1933
      1935
      1983
      1990


      How to get the average?







      bash unix shell average






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 10 at 18:15









      Kamil Maciorowski

      24.1k155176




      24.1k155176










      asked Dec 10 at 14:14









      Alex

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          1














          If the only thing in your file are numbers, you can use a single awk command to get everything.



          awk 'BEGIN {themin=10000000; themax=0; thecount=0; thesum=0}
          {for (i=1; i<=NF; i++) {
          thesum += $i;
          thecount++;
          if ($i < themin) {themin = $i}
          if ($i > themax) {themax = $i}
          }}
          END {
          printf("The min is %dnThe max is %dnThe sum is %dnThe total number of items is %dnThe average of those items is %dn", themin, themax, thesum, thecount, int(thesum/thecount))
          }' _file_


          I broke it out so you can see the different sections.



          The BEGIN is just initializing all the variables to be used. The variable, themin, can be set to whatever large value you want, as long as it is larger than anything in your file.



          The middle part just loops through each line, and each field in that line. It is summing up the numbers, and getting a count of all of the numbers so a simple division can be done at the end. The two if statements collect your min and max values.



          The END prints out all of the relevant pieces of information, including your min, max and average.



          Hope this helps.






          share|improve this answer





























            0














            No need to write your own solutions for problems that already have splendid solutions. After all, you asked on SuperUser and not on StackOverflow.



            For instance use GNU datamash



            $ datamash min 1 max 1 mean 1 < yourFile
            233 1990 1272.0408163265


            or use the tools from the package num-utils



            $ numbound -l yourFile
            233
            $ numbound yourFile
            1990
            $ numaverage yourFile
            1272.04081632653





            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fsuperuser.com%2fquestions%2f1382328%2faverage-of-multiple-numbers-shell-script%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














              If the only thing in your file are numbers, you can use a single awk command to get everything.



              awk 'BEGIN {themin=10000000; themax=0; thecount=0; thesum=0}
              {for (i=1; i<=NF; i++) {
              thesum += $i;
              thecount++;
              if ($i < themin) {themin = $i}
              if ($i > themax) {themax = $i}
              }}
              END {
              printf("The min is %dnThe max is %dnThe sum is %dnThe total number of items is %dnThe average of those items is %dn", themin, themax, thesum, thecount, int(thesum/thecount))
              }' _file_


              I broke it out so you can see the different sections.



              The BEGIN is just initializing all the variables to be used. The variable, themin, can be set to whatever large value you want, as long as it is larger than anything in your file.



              The middle part just loops through each line, and each field in that line. It is summing up the numbers, and getting a count of all of the numbers so a simple division can be done at the end. The two if statements collect your min and max values.



              The END prints out all of the relevant pieces of information, including your min, max and average.



              Hope this helps.






              share|improve this answer


























                1














                If the only thing in your file are numbers, you can use a single awk command to get everything.



                awk 'BEGIN {themin=10000000; themax=0; thecount=0; thesum=0}
                {for (i=1; i<=NF; i++) {
                thesum += $i;
                thecount++;
                if ($i < themin) {themin = $i}
                if ($i > themax) {themax = $i}
                }}
                END {
                printf("The min is %dnThe max is %dnThe sum is %dnThe total number of items is %dnThe average of those items is %dn", themin, themax, thesum, thecount, int(thesum/thecount))
                }' _file_


                I broke it out so you can see the different sections.



                The BEGIN is just initializing all the variables to be used. The variable, themin, can be set to whatever large value you want, as long as it is larger than anything in your file.



                The middle part just loops through each line, and each field in that line. It is summing up the numbers, and getting a count of all of the numbers so a simple division can be done at the end. The two if statements collect your min and max values.



                The END prints out all of the relevant pieces of information, including your min, max and average.



                Hope this helps.






                share|improve this answer
























                  1












                  1








                  1






                  If the only thing in your file are numbers, you can use a single awk command to get everything.



                  awk 'BEGIN {themin=10000000; themax=0; thecount=0; thesum=0}
                  {for (i=1; i<=NF; i++) {
                  thesum += $i;
                  thecount++;
                  if ($i < themin) {themin = $i}
                  if ($i > themax) {themax = $i}
                  }}
                  END {
                  printf("The min is %dnThe max is %dnThe sum is %dnThe total number of items is %dnThe average of those items is %dn", themin, themax, thesum, thecount, int(thesum/thecount))
                  }' _file_


                  I broke it out so you can see the different sections.



                  The BEGIN is just initializing all the variables to be used. The variable, themin, can be set to whatever large value you want, as long as it is larger than anything in your file.



                  The middle part just loops through each line, and each field in that line. It is summing up the numbers, and getting a count of all of the numbers so a simple division can be done at the end. The two if statements collect your min and max values.



                  The END prints out all of the relevant pieces of information, including your min, max and average.



                  Hope this helps.






                  share|improve this answer












                  If the only thing in your file are numbers, you can use a single awk command to get everything.



                  awk 'BEGIN {themin=10000000; themax=0; thecount=0; thesum=0}
                  {for (i=1; i<=NF; i++) {
                  thesum += $i;
                  thecount++;
                  if ($i < themin) {themin = $i}
                  if ($i > themax) {themax = $i}
                  }}
                  END {
                  printf("The min is %dnThe max is %dnThe sum is %dnThe total number of items is %dnThe average of those items is %dn", themin, themax, thesum, thecount, int(thesum/thecount))
                  }' _file_


                  I broke it out so you can see the different sections.



                  The BEGIN is just initializing all the variables to be used. The variable, themin, can be set to whatever large value you want, as long as it is larger than anything in your file.



                  The middle part just loops through each line, and each field in that line. It is summing up the numbers, and getting a count of all of the numbers so a simple division can be done at the end. The two if statements collect your min and max values.



                  The END prints out all of the relevant pieces of information, including your min, max and average.



                  Hope this helps.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 10 at 15:50









                  Lewis M

                  3175




                  3175

























                      0














                      No need to write your own solutions for problems that already have splendid solutions. After all, you asked on SuperUser and not on StackOverflow.



                      For instance use GNU datamash



                      $ datamash min 1 max 1 mean 1 < yourFile
                      233 1990 1272.0408163265


                      or use the tools from the package num-utils



                      $ numbound -l yourFile
                      233
                      $ numbound yourFile
                      1990
                      $ numaverage yourFile
                      1272.04081632653





                      share|improve this answer


























                        0














                        No need to write your own solutions for problems that already have splendid solutions. After all, you asked on SuperUser and not on StackOverflow.



                        For instance use GNU datamash



                        $ datamash min 1 max 1 mean 1 < yourFile
                        233 1990 1272.0408163265


                        or use the tools from the package num-utils



                        $ numbound -l yourFile
                        233
                        $ numbound yourFile
                        1990
                        $ numaverage yourFile
                        1272.04081632653





                        share|improve this answer
























                          0












                          0








                          0






                          No need to write your own solutions for problems that already have splendid solutions. After all, you asked on SuperUser and not on StackOverflow.



                          For instance use GNU datamash



                          $ datamash min 1 max 1 mean 1 < yourFile
                          233 1990 1272.0408163265


                          or use the tools from the package num-utils



                          $ numbound -l yourFile
                          233
                          $ numbound yourFile
                          1990
                          $ numaverage yourFile
                          1272.04081632653





                          share|improve this answer












                          No need to write your own solutions for problems that already have splendid solutions. After all, you asked on SuperUser and not on StackOverflow.



                          For instance use GNU datamash



                          $ datamash min 1 max 1 mean 1 < yourFile
                          233 1990 1272.0408163265


                          or use the tools from the package num-utils



                          $ numbound -l yourFile
                          233
                          $ numbound yourFile
                          1990
                          $ numaverage yourFile
                          1272.04081632653






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 11 at 18:12









                          Socowi

                          1396




                          1396






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Super User!


                              • 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%2fsuperuser.com%2fquestions%2f1382328%2faverage-of-multiple-numbers-shell-script%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

                              Paul Cézanne

                              UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                              Angular material date-picker (MatDatepicker) auto completes the date on focus out