Sum and count in for loop

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












1















I have five files each with a number in the first line.



I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.



This is what I have attempted so far:



for file in $* 
do
$[head -1 $file]
echo $(head -1)
done


I am unsure how to incorporate a sum and count element as yet.










share|improve this question






















  • Can you provide example context of file and expected output?

    – Romeo Ninov
    Feb 6 at 12:16











  • I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).

    – marzo
    Feb 6 at 21:35
















1















I have five files each with a number in the first line.



I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.



This is what I have attempted so far:



for file in $* 
do
$[head -1 $file]
echo $(head -1)
done


I am unsure how to incorporate a sum and count element as yet.










share|improve this question






















  • Can you provide example context of file and expected output?

    – Romeo Ninov
    Feb 6 at 12:16











  • I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).

    – marzo
    Feb 6 at 21:35














1












1








1








I have five files each with a number in the first line.



I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.



This is what I have attempted so far:



for file in $* 
do
$[head -1 $file]
echo $(head -1)
done


I am unsure how to incorporate a sum and count element as yet.










share|improve this question














I have five files each with a number in the first line.



I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.



This is what I have attempted so far:



for file in $* 
do
$[head -1 $file]
echo $(head -1)
done


I am unsure how to incorporate a sum and count element as yet.







shell-script files for






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 6 at 11:47









marzomarzo

132




132












  • Can you provide example context of file and expected output?

    – Romeo Ninov
    Feb 6 at 12:16











  • I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).

    – marzo
    Feb 6 at 21:35


















  • Can you provide example context of file and expected output?

    – Romeo Ninov
    Feb 6 at 12:16











  • I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).

    – marzo
    Feb 6 at 21:35

















Can you provide example context of file and expected output?

– Romeo Ninov
Feb 6 at 12:16





Can you provide example context of file and expected output?

– Romeo Ninov
Feb 6 at 12:16













I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).

– marzo
Feb 6 at 21:35






I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).

– marzo
Feb 6 at 21:35











2 Answers
2






active

oldest

votes


















2














I guess what you are trying to do can be achieved by this:



#!/bin/bash
sum=0
count=0
for file in "$@"
do
number=$(head -1 "$file")
count=$((count + 1))
sum=$((sum + number))
done
echo "The sum of the $count file(s) is: $sum"


You just add variables to store the sum and the count of the files you are working with. You then increase the count each time you run through the loop. Also we add the number at the beginning line of the file to our sum-variable.






share|improve this answer
































    2














    You could also do this with awk, avoiding the need to for head for each file:



    gawk 'count += 1; sum += $1; nextfile 
    END printf "count: %dt sum: %dn", count, sum' *


    The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END, we print out the numbers.



    nextfile is a GNU thing, it might not work in other versions of awk. Another alternative would be to explicitly work only on the first line:



    awk 'FNR == 1 count += 1; sum += $1 
    END printf "count: %dt sum: %dn", count, sum' *


    This would however read the files in full, even if it doesn't do anything with the rest of the lines.






    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%2f499027%2fsum-and-count-in-for-loop%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









      2














      I guess what you are trying to do can be achieved by this:



      #!/bin/bash
      sum=0
      count=0
      for file in "$@"
      do
      number=$(head -1 "$file")
      count=$((count + 1))
      sum=$((sum + number))
      done
      echo "The sum of the $count file(s) is: $sum"


      You just add variables to store the sum and the count of the files you are working with. You then increase the count each time you run through the loop. Also we add the number at the beginning line of the file to our sum-variable.






      share|improve this answer





























        2














        I guess what you are trying to do can be achieved by this:



        #!/bin/bash
        sum=0
        count=0
        for file in "$@"
        do
        number=$(head -1 "$file")
        count=$((count + 1))
        sum=$((sum + number))
        done
        echo "The sum of the $count file(s) is: $sum"


        You just add variables to store the sum and the count of the files you are working with. You then increase the count each time you run through the loop. Also we add the number at the beginning line of the file to our sum-variable.






        share|improve this answer



























          2












          2








          2







          I guess what you are trying to do can be achieved by this:



          #!/bin/bash
          sum=0
          count=0
          for file in "$@"
          do
          number=$(head -1 "$file")
          count=$((count + 1))
          sum=$((sum + number))
          done
          echo "The sum of the $count file(s) is: $sum"


          You just add variables to store the sum and the count of the files you are working with. You then increase the count each time you run through the loop. Also we add the number at the beginning line of the file to our sum-variable.






          share|improve this answer















          I guess what you are trying to do can be achieved by this:



          #!/bin/bash
          sum=0
          count=0
          for file in "$@"
          do
          number=$(head -1 "$file")
          count=$((count + 1))
          sum=$((sum + number))
          done
          echo "The sum of the $count file(s) is: $sum"


          You just add variables to store the sum and the count of the files you are working with. You then increase the count each time you run through the loop. Also we add the number at the beginning line of the file to our sum-variable.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 6 at 12:55









          ilkkachu

          60.1k998171




          60.1k998171










          answered Feb 6 at 12:48









          majesticLSDmajesticLSD

          763




          763























              2














              You could also do this with awk, avoiding the need to for head for each file:



              gawk 'count += 1; sum += $1; nextfile 
              END printf "count: %dt sum: %dn", count, sum' *


              The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END, we print out the numbers.



              nextfile is a GNU thing, it might not work in other versions of awk. Another alternative would be to explicitly work only on the first line:



              awk 'FNR == 1 count += 1; sum += $1 
              END printf "count: %dt sum: %dn", count, sum' *


              This would however read the files in full, even if it doesn't do anything with the rest of the lines.






              share|improve this answer





























                2














                You could also do this with awk, avoiding the need to for head for each file:



                gawk 'count += 1; sum += $1; nextfile 
                END printf "count: %dt sum: %dn", count, sum' *


                The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END, we print out the numbers.



                nextfile is a GNU thing, it might not work in other versions of awk. Another alternative would be to explicitly work only on the first line:



                awk 'FNR == 1 count += 1; sum += $1 
                END printf "count: %dt sum: %dn", count, sum' *


                This would however read the files in full, even if it doesn't do anything with the rest of the lines.






                share|improve this answer



























                  2












                  2








                  2







                  You could also do this with awk, avoiding the need to for head for each file:



                  gawk 'count += 1; sum += $1; nextfile 
                  END printf "count: %dt sum: %dn", count, sum' *


                  The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END, we print out the numbers.



                  nextfile is a GNU thing, it might not work in other versions of awk. Another alternative would be to explicitly work only on the first line:



                  awk 'FNR == 1 count += 1; sum += $1 
                  END printf "count: %dt sum: %dn", count, sum' *


                  This would however read the files in full, even if it doesn't do anything with the rest of the lines.






                  share|improve this answer















                  You could also do this with awk, avoiding the need to for head for each file:



                  gawk 'count += 1; sum += $1; nextfile 
                  END printf "count: %dt sum: %dn", count, sum' *


                  The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END, we print out the numbers.



                  nextfile is a GNU thing, it might not work in other versions of awk. Another alternative would be to explicitly work only on the first line:



                  awk 'FNR == 1 count += 1; sum += $1 
                  END printf "count: %dt sum: %dn", count, sum' *


                  This would however read the files in full, even if it doesn't do anything with the rest of the lines.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 6 at 13:28









                  Kusalananda

                  133k17253416




                  133k17253416










                  answered Feb 6 at 13:00









                  ilkkachuilkkachu

                  60.1k998171




                  60.1k998171



























                      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%2f499027%2fsum-and-count-in-for-loop%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

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?