awk + sum all numbersIs there a way to sum up the size of files listed?How can I quickly sum all numbers in a...

Does SQL Server 2017, including older versions, support 8K disk sector sizes?

A title for a history book

Replacement expressions

Why do stocks necessarily drop during a recession?

How can my powered armor quickly replace its ceramic plates?

Can you share a component pouch with another creature?

Transpose a matrix and parenthesis

How would an AI self awareness kill switch work?

MAC Address learning process

What's a good word to describe a public place that looks like it wouldn't be rough?

How to prevent users from executing commands through browser URL

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

Making him into a bully (how to show mild violence)

Why is it that Bernie Sanders is always called a "socialist"?

Why is working on the same position for more than 15 years not a red flag?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Could a phylactery of a lich be a mirror or does it have to be a box?

Is boss over stepping boundary/micromanaging?

Why would space fleets be aligned?

Why avoid shared user accounts?

finding the number of complex number

Why would Pakistan closing its air space cancel flights not headed to Pakistan itself?

Porting Linux to another platform requirements

Why is the copy constructor called twice in this code snippet?



awk + sum all numbers


Is there a way to sum up the size of files listed?How can I quickly sum all numbers in a file?AWK sum column in file specify as a argumentHow to sum many numbers inside 2D array using awkawk + count field separator in csv and print line numberHow to join duplicates and sum their numbers with awkawk to sum the numbers(floating) and group it on a unique keySumming rows in a new column using sed, awk and perl?Check which process is using most memory and summary total used memoryAWK how to count sumSum and count in for loop













2















We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?










share|improve this question

























  • du -bs /tmp would get you the answer too

    – roaima
    44 mins ago













  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    42 mins ago











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    40 mins ago


















2















We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?










share|improve this question

























  • du -bs /tmp would get you the answer too

    – roaima
    44 mins ago













  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    42 mins ago











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    40 mins ago
















2












2








2


1






We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?










share|improve this question
















We want to calculate the first numbers that we get from du



du -b /tmp/*
6 /tmp/216c6f99-6671-4865-b8bc-7205f5388752_resources
668669 /tmp/hadoop7887078727316788325.tmp
6 /tmp/hadoop-hdfs
42456 /tmp/hive
32786 /tmp/hsperfdata_hdfs
6 /tmp/hsperfdata_hive
32786 /tmp/hsperfdata_root
262244 /tmp/hsperfdata_yarn


so final sum will be



sum=6+668669+6+42456+32786+6+32786+262244


echo $sum


How we can do it by awk or perl one liners?







linux shell-script awk perl disk-usage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 23 mins ago









PRY

2,57831026




2,57831026










asked 48 mins ago









yaelyael

2,63722571




2,63722571













  • du -bs /tmp would get you the answer too

    – roaima
    44 mins ago













  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    42 mins ago











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    40 mins ago





















  • du -bs /tmp would get you the answer too

    – roaima
    44 mins ago













  • See How can I quickly sum all numbers in a file?

    – glenn jackman
    42 mins ago











  • See also Is there a way to sum up the size of files listed?

    – Jeff Schaller
    40 mins ago



















du -bs /tmp would get you the answer too

– roaima
44 mins ago







du -bs /tmp would get you the answer too

– roaima
44 mins ago















See How can I quickly sum all numbers in a file?

– glenn jackman
42 mins ago





See How can I quickly sum all numbers in a file?

– glenn jackman
42 mins ago













See also Is there a way to sum up the size of files listed?

– Jeff Schaller
40 mins ago







See also Is there a way to sum up the size of files listed?

– Jeff Schaller
40 mins ago












3 Answers
3






active

oldest

votes


















4














In AWK:



{ sum += $1 }
END { print sum }


So



du -b /tmp/* | awk '{ sum += $1 } END { print sum }'


du -s will also calculate the sum for you (on all subdirectories and files in /tmp, including hidden ones):



du -sb /tmp





share|improve this answer

































    2














    It is simple you can use:



     du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print i}'


    If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



    size1 file1
    size2 file2
    size_total .


    So now you should avoid this last entry, so use:



    du -b /tmp | awk 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print i}'


    However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



    du -s directory





    share|improve this answer





















    • 1





      variables initialize to zero, if you'd like to golf some bytes off :)

      – Jeff Schaller
      42 mins ago



















    -1














    You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



    $ du -sb file1 file2
    17 file1
    18 file2

    $ du -cb file1 file2
    17 file1
    18 file2
    35 total


    BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



    $ du -ch file1 file2
    4.0K file1
    4.0K file2
    8.0K total





    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%2f503601%2fawk-sum-all-numbers%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      In AWK:



      { sum += $1 }
      END { print sum }


      So



      du -b /tmp/* | awk '{ sum += $1 } END { print sum }'


      du -s will also calculate the sum for you (on all subdirectories and files in /tmp, including hidden ones):



      du -sb /tmp





      share|improve this answer






























        4














        In AWK:



        { sum += $1 }
        END { print sum }


        So



        du -b /tmp/* | awk '{ sum += $1 } END { print sum }'


        du -s will also calculate the sum for you (on all subdirectories and files in /tmp, including hidden ones):



        du -sb /tmp





        share|improve this answer




























          4












          4








          4







          In AWK:



          { sum += $1 }
          END { print sum }


          So



          du -b /tmp/* | awk '{ sum += $1 } END { print sum }'


          du -s will also calculate the sum for you (on all subdirectories and files in /tmp, including hidden ones):



          du -sb /tmp





          share|improve this answer















          In AWK:



          { sum += $1 }
          END { print sum }


          So



          du -b /tmp/* | awk '{ sum += $1 } END { print sum }'


          du -s will also calculate the sum for you (on all subdirectories and files in /tmp, including hidden ones):



          du -sb /tmp






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 38 mins ago

























          answered 44 mins ago









          Stephen KittStephen Kitt

          174k24397472




          174k24397472

























              2














              It is simple you can use:



               du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print i}'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print i}'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory





              share|improve this answer





















              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                42 mins ago
















              2














              It is simple you can use:



               du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print i}'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print i}'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory





              share|improve this answer





















              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                42 mins ago














              2












              2








              2







              It is simple you can use:



               du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print i}'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print i}'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory





              share|improve this answer















              It is simple you can use:



               du -b /tmp/* | awk 'BEGIN{i=0} {i=i+$1} END{print i}'


              If you are not using wildcard, if you are using directory name like /tmp, then you need to avoid the last entry because output of du -b /tmp is like:



              size1 file1
              size2 file2
              size_total .


              So now you should avoid this last entry, so use:



              du -b /tmp | awk 'BEGIN{i=0} {if( $2 != "." ){i=i+$1}} END{print i}'


              However you can also use -s option, it will calculate the summary for you then you don't need to add the values, just print the last one, i.e.:



              du -s directory






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 20 mins ago

























              answered 43 mins ago









              PRYPRY

              2,57831026




              2,57831026








              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                42 mins ago














              • 1





                variables initialize to zero, if you'd like to golf some bytes off :)

                – Jeff Schaller
                42 mins ago








              1




              1





              variables initialize to zero, if you'd like to golf some bytes off :)

              – Jeff Schaller
              42 mins ago





              variables initialize to zero, if you'd like to golf some bytes off :)

              – Jeff Schaller
              42 mins ago











              -1














              You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



              $ du -sb file1 file2
              17 file1
              18 file2

              $ du -cb file1 file2
              17 file1
              18 file2
              35 total


              BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



              $ du -ch file1 file2
              4.0K file1
              4.0K file2
              8.0K total





              share|improve this answer




























                -1














                You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



                $ du -sb file1 file2
                17 file1
                18 file2

                $ du -cb file1 file2
                17 file1
                18 file2
                35 total


                BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



                $ du -ch file1 file2
                4.0K file1
                4.0K file2
                8.0K total





                share|improve this answer


























                  -1












                  -1








                  -1







                  You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



                  $ du -sb file1 file2
                  17 file1
                  18 file2

                  $ du -cb file1 file2
                  17 file1
                  18 file2
                  35 total


                  BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



                  $ du -ch file1 file2
                  4.0K file1
                  4.0K file2
                  8.0K total





                  share|improve this answer













                  You can also produce a total sum of selected files with du -c. This works even if an argument of du is not a directory, what is not the case of du -s:



                  $ du -sb file1 file2
                  17 file1
                  18 file2

                  $ du -cb file1 file2
                  17 file1
                  18 file2
                  35 total


                  BTW, for interactive use I recommend adding -h option instead of -b or any other multiplier of block-size. This will print the size in human readable unit format.



                  $ du -ch file1 file2
                  4.0K file1
                  4.0K file2
                  8.0K total






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 11 mins ago









                  jimmijjimmij

                  31.9k874108




                  31.9k874108






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503601%2fawk-sum-all-numbers%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

                      Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

                      Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

                      Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV