Sum the values in a column except the header












7















I have a file as given below



--------------------------------------------------------------
Name_Customer Item_Purchased Item_Amount Credit
--------------------------------------------------------------
Tom H1_P 7657 N/A
Pras Track_1 23 N/A
Cha Brace 9 N/A
Moh kite37 269 N/A
Prab Bols 87699 N/A


I need to add the values under the column Item_Amount by ignoring the header in the file and print the sum as



Total Amount collected = 95657









share|improve this question





























    7















    I have a file as given below



    --------------------------------------------------------------
    Name_Customer Item_Purchased Item_Amount Credit
    --------------------------------------------------------------
    Tom H1_P 7657 N/A
    Pras Track_1 23 N/A
    Cha Brace 9 N/A
    Moh kite37 269 N/A
    Prab Bols 87699 N/A


    I need to add the values under the column Item_Amount by ignoring the header in the file and print the sum as



    Total Amount collected = 95657









    share|improve this question



























      7












      7








      7








      I have a file as given below



      --------------------------------------------------------------
      Name_Customer Item_Purchased Item_Amount Credit
      --------------------------------------------------------------
      Tom H1_P 7657 N/A
      Pras Track_1 23 N/A
      Cha Brace 9 N/A
      Moh kite37 269 N/A
      Prab Bols 87699 N/A


      I need to add the values under the column Item_Amount by ignoring the header in the file and print the sum as



      Total Amount collected = 95657









      share|improve this question
















      I have a file as given below



      --------------------------------------------------------------
      Name_Customer Item_Purchased Item_Amount Credit
      --------------------------------------------------------------
      Tom H1_P 7657 N/A
      Pras Track_1 23 N/A
      Cha Brace 9 N/A
      Moh kite37 269 N/A
      Prab Bols 87699 N/A


      I need to add the values under the column Item_Amount by ignoring the header in the file and print the sum as



      Total Amount collected = 95657






      text-processing ksh columns






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 14 '13 at 22:50









      Gilles

      534k12810781595




      534k12810781595










      asked Oct 14 '13 at 19:29









      RamRam

      1833411




      1833411






















          7 Answers
          7






          active

          oldest

          votes


















          10














          awk '{s+=$3}END{print s}' yourfile





          share|improve this answer


























          • @coffeMug can you explain why use this: s+0?.

            – Cold
            Dec 15 '14 at 15:32











          • @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

            – coffeMug
            Dec 15 '14 at 19:43











          • @coffeMug probably to ensure awk treated the argument as a number

            – Thorbjørn Ravn Andersen
            May 9 '17 at 11:40



















          4














          Pretty trivial using just awk. Assuming the example data is in a file, ex.txt:



          $ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt


          Example



          $ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt 
          Total Amount collected = 95657


          Details



          Using awk we collect the values from the 3rd column ($3) and accumulate their sub-total in the variable total. Once complete, as the last thing to do, END{..}, we print the message along with the value of the variable total.






          share|improve this answer


























          • just make one changes int($3)

            – Rahul Patil
            Oct 15 '13 at 6:45











          • @RahulPatil - thanks, changed it.

            – slm
            Oct 15 '13 at 6:47



















          1














          The awk approach is probably the easiest. Here are a few other choices:



          Perl:



          perl -lane '$k+=$F[2];END{print $k}' foo.txt


          Pure coreutils:



          t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 | 
          while read i; do let t+=$i; echo $t; done | tail -n 1





          share|improve this answer

































            1














            If it can help:



            grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l





            share|improve this answer

































              0














              total=0; 
              for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
              do
              total=$( expr $total + $n );
              done ;
              echo ">>$total"





              share|improve this answer





















              • 1





                You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                – terdon
                Oct 14 '13 at 20:57



















              0














              This pipeline should do the work:



              tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'





              share|improve this answer

































                -1














                awk '{FS=","}{s+=$6}END{print s}' Filename





                share|improve this answer





















                • 1





                  Can you explain it?

                  – P_Yadav
                  Jan 22 at 13:19











                • Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                  – Archemar
                  Jan 22 at 13:37











                • Given that there are no commas in the sample input, how does this work?

                  – Jeff Schaller
                  Jan 22 at 13:54











                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%2f96031%2fsum-the-values-in-a-column-except-the-header%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                7 Answers
                7






                active

                oldest

                votes








                7 Answers
                7






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                10














                awk '{s+=$3}END{print s}' yourfile





                share|improve this answer


























                • @coffeMug can you explain why use this: s+0?.

                  – Cold
                  Dec 15 '14 at 15:32











                • @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

                  – coffeMug
                  Dec 15 '14 at 19:43











                • @coffeMug probably to ensure awk treated the argument as a number

                  – Thorbjørn Ravn Andersen
                  May 9 '17 at 11:40
















                10














                awk '{s+=$3}END{print s}' yourfile





                share|improve this answer


























                • @coffeMug can you explain why use this: s+0?.

                  – Cold
                  Dec 15 '14 at 15:32











                • @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

                  – coffeMug
                  Dec 15 '14 at 19:43











                • @coffeMug probably to ensure awk treated the argument as a number

                  – Thorbjørn Ravn Andersen
                  May 9 '17 at 11:40














                10












                10








                10







                awk '{s+=$3}END{print s}' yourfile





                share|improve this answer















                awk '{s+=$3}END{print s}' yourfile






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 15 '14 at 19:44

























                answered Oct 14 '13 at 19:39









                coffeMugcoffeMug

                7,188112748




                7,188112748













                • @coffeMug can you explain why use this: s+0?.

                  – Cold
                  Dec 15 '14 at 15:32











                • @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

                  – coffeMug
                  Dec 15 '14 at 19:43











                • @coffeMug probably to ensure awk treated the argument as a number

                  – Thorbjørn Ravn Andersen
                  May 9 '17 at 11:40



















                • @coffeMug can you explain why use this: s+0?.

                  – Cold
                  Dec 15 '14 at 15:32











                • @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

                  – coffeMug
                  Dec 15 '14 at 19:43











                • @coffeMug probably to ensure awk treated the argument as a number

                  – Thorbjørn Ravn Andersen
                  May 9 '17 at 11:40

















                @coffeMug can you explain why use this: s+0?.

                – Cold
                Dec 15 '14 at 15:32





                @coffeMug can you explain why use this: s+0?.

                – Cold
                Dec 15 '14 at 15:32













                @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

                – coffeMug
                Dec 15 '14 at 19:43





                @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!

                – coffeMug
                Dec 15 '14 at 19:43













                @coffeMug probably to ensure awk treated the argument as a number

                – Thorbjørn Ravn Andersen
                May 9 '17 at 11:40





                @coffeMug probably to ensure awk treated the argument as a number

                – Thorbjørn Ravn Andersen
                May 9 '17 at 11:40













                4














                Pretty trivial using just awk. Assuming the example data is in a file, ex.txt:



                $ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt


                Example



                $ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt 
                Total Amount collected = 95657


                Details



                Using awk we collect the values from the 3rd column ($3) and accumulate their sub-total in the variable total. Once complete, as the last thing to do, END{..}, we print the message along with the value of the variable total.






                share|improve this answer


























                • just make one changes int($3)

                  – Rahul Patil
                  Oct 15 '13 at 6:45











                • @RahulPatil - thanks, changed it.

                  – slm
                  Oct 15 '13 at 6:47
















                4














                Pretty trivial using just awk. Assuming the example data is in a file, ex.txt:



                $ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt


                Example



                $ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt 
                Total Amount collected = 95657


                Details



                Using awk we collect the values from the 3rd column ($3) and accumulate their sub-total in the variable total. Once complete, as the last thing to do, END{..}, we print the message along with the value of the variable total.






                share|improve this answer


























                • just make one changes int($3)

                  – Rahul Patil
                  Oct 15 '13 at 6:45











                • @RahulPatil - thanks, changed it.

                  – slm
                  Oct 15 '13 at 6:47














                4












                4








                4







                Pretty trivial using just awk. Assuming the example data is in a file, ex.txt:



                $ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt


                Example



                $ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt 
                Total Amount collected = 95657


                Details



                Using awk we collect the values from the 3rd column ($3) and accumulate their sub-total in the variable total. Once complete, as the last thing to do, END{..}, we print the message along with the value of the variable total.






                share|improve this answer















                Pretty trivial using just awk. Assuming the example data is in a file, ex.txt:



                $ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt


                Example



                $ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt 
                Total Amount collected = 95657


                Details



                Using awk we collect the values from the 3rd column ($3) and accumulate their sub-total in the variable total. Once complete, as the last thing to do, END{..}, we print the message along with the value of the variable total.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 15 '13 at 6:47

























                answered Oct 15 '13 at 6:39









                slmslm

                249k66523682




                249k66523682













                • just make one changes int($3)

                  – Rahul Patil
                  Oct 15 '13 at 6:45











                • @RahulPatil - thanks, changed it.

                  – slm
                  Oct 15 '13 at 6:47



















                • just make one changes int($3)

                  – Rahul Patil
                  Oct 15 '13 at 6:45











                • @RahulPatil - thanks, changed it.

                  – slm
                  Oct 15 '13 at 6:47

















                just make one changes int($3)

                – Rahul Patil
                Oct 15 '13 at 6:45





                just make one changes int($3)

                – Rahul Patil
                Oct 15 '13 at 6:45













                @RahulPatil - thanks, changed it.

                – slm
                Oct 15 '13 at 6:47





                @RahulPatil - thanks, changed it.

                – slm
                Oct 15 '13 at 6:47











                1














                The awk approach is probably the easiest. Here are a few other choices:



                Perl:



                perl -lane '$k+=$F[2];END{print $k}' foo.txt


                Pure coreutils:



                t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 | 
                while read i; do let t+=$i; echo $t; done | tail -n 1





                share|improve this answer






























                  1














                  The awk approach is probably the easiest. Here are a few other choices:



                  Perl:



                  perl -lane '$k+=$F[2];END{print $k}' foo.txt


                  Pure coreutils:



                  t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 | 
                  while read i; do let t+=$i; echo $t; done | tail -n 1





                  share|improve this answer




























                    1












                    1








                    1







                    The awk approach is probably the easiest. Here are a few other choices:



                    Perl:



                    perl -lane '$k+=$F[2];END{print $k}' foo.txt


                    Pure coreutils:



                    t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 | 
                    while read i; do let t+=$i; echo $t; done | tail -n 1





                    share|improve this answer















                    The awk approach is probably the easiest. Here are a few other choices:



                    Perl:



                    perl -lane '$k+=$F[2];END{print $k}' foo.txt


                    Pure coreutils:



                    t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 | 
                    while read i; do let t+=$i; echo $t; done | tail -n 1






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 14 '13 at 20:02

























                    answered Oct 14 '13 at 19:50









                    terdonterdon

                    130k32254432




                    130k32254432























                        1














                        If it can help:



                        grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l





                        share|improve this answer






























                          1














                          If it can help:



                          grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l





                          share|improve this answer




























                            1












                            1








                            1







                            If it can help:



                            grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l





                            share|improve this answer















                            If it can help:



                            grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Apr 4 '18 at 15:01









                            datUser

                            2,5061133




                            2,5061133










                            answered Apr 4 '18 at 14:53









                            K-melK-mel

                            111




                            111























                                0














                                total=0; 
                                for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
                                do
                                total=$( expr $total + $n );
                                done ;
                                echo ">>$total"





                                share|improve this answer





















                                • 1





                                  You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                                  – terdon
                                  Oct 14 '13 at 20:57
















                                0














                                total=0; 
                                for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
                                do
                                total=$( expr $total + $n );
                                done ;
                                echo ">>$total"





                                share|improve this answer





















                                • 1





                                  You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                                  – terdon
                                  Oct 14 '13 at 20:57














                                0












                                0








                                0







                                total=0; 
                                for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
                                do
                                total=$( expr $total + $n );
                                done ;
                                echo ">>$total"





                                share|improve this answer















                                total=0; 
                                for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
                                do
                                total=$( expr $total + $n );
                                done ;
                                echo ">>$total"






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Oct 14 '13 at 19:50









                                terdon

                                130k32254432




                                130k32254432










                                answered Oct 14 '13 at 19:44









                                josejose

                                11




                                11








                                • 1





                                  You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                                  – terdon
                                  Oct 14 '13 at 20:57














                                • 1





                                  You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                                  – terdon
                                  Oct 14 '13 at 20:57








                                1




                                1





                                You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                                – terdon
                                Oct 14 '13 at 20:57





                                You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s

                                – terdon
                                Oct 14 '13 at 20:57











                                0














                                This pipeline should do the work:



                                tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'





                                share|improve this answer






























                                  0














                                  This pipeline should do the work:



                                  tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'





                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    This pipeline should do the work:



                                    tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'





                                    share|improve this answer















                                    This pipeline should do the work:



                                    tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Oct 14 '13 at 22:43

























                                    answered Oct 14 '13 at 22:29







                                    user13742






























                                        -1














                                        awk '{FS=","}{s+=$6}END{print s}' Filename





                                        share|improve this answer





















                                        • 1





                                          Can you explain it?

                                          – P_Yadav
                                          Jan 22 at 13:19











                                        • Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                                          – Archemar
                                          Jan 22 at 13:37











                                        • Given that there are no commas in the sample input, how does this work?

                                          – Jeff Schaller
                                          Jan 22 at 13:54
















                                        -1














                                        awk '{FS=","}{s+=$6}END{print s}' Filename





                                        share|improve this answer





















                                        • 1





                                          Can you explain it?

                                          – P_Yadav
                                          Jan 22 at 13:19











                                        • Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                                          – Archemar
                                          Jan 22 at 13:37











                                        • Given that there are no commas in the sample input, how does this work?

                                          – Jeff Schaller
                                          Jan 22 at 13:54














                                        -1












                                        -1








                                        -1







                                        awk '{FS=","}{s+=$6}END{print s}' Filename





                                        share|improve this answer















                                        awk '{FS=","}{s+=$6}END{print s}' Filename






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Jan 22 at 13:54









                                        Jeff Schaller

                                        40.6k1056129




                                        40.6k1056129










                                        answered Jan 22 at 13:14









                                        Madhu KMadhu K

                                        1




                                        1








                                        • 1





                                          Can you explain it?

                                          – P_Yadav
                                          Jan 22 at 13:19











                                        • Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                                          – Archemar
                                          Jan 22 at 13:37











                                        • Given that there are no commas in the sample input, how does this work?

                                          – Jeff Schaller
                                          Jan 22 at 13:54














                                        • 1





                                          Can you explain it?

                                          – P_Yadav
                                          Jan 22 at 13:19











                                        • Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                                          – Archemar
                                          Jan 22 at 13:37











                                        • Given that there are no commas in the sample input, how does this work?

                                          – Jeff Schaller
                                          Jan 22 at 13:54








                                        1




                                        1





                                        Can you explain it?

                                        – P_Yadav
                                        Jan 22 at 13:19





                                        Can you explain it?

                                        – P_Yadav
                                        Jan 22 at 13:19













                                        Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                                        – Archemar
                                        Jan 22 at 13:37





                                        Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.

                                        – Archemar
                                        Jan 22 at 13:37













                                        Given that there are no commas in the sample input, how does this work?

                                        – Jeff Schaller
                                        Jan 22 at 13:54





                                        Given that there are no commas in the sample input, how does this work?

                                        – Jeff Schaller
                                        Jan 22 at 13:54


















                                        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%2f96031%2fsum-the-values-in-a-column-except-the-header%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 reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                        is 'sed' thread safe

                                        How to make a Squid Proxy server?