How to grep and cut numbers from a file and sum them The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionGrep alias - line numbers unless it's in a pipelineIssue with real-time log inspecting piping tail, grep and cutcopy every line from a text file that contains a number greater than 5000How to unbuffer cut?Grep to filter and show only the beginning of a lineTail Grep - Print surrounding lines until pattern is matchedGrep and filter IP from text fileawk/sed/grep: Printing all lines matching a string and all lines with tabs after these linesGrep only numbers, not the alphanumeric entriesHow do I add numbers from two txt files with Bash?

What are these Gizmos at Izaña Atmospheric Research Center in Spain?

How to stretch delimiters to envolve matrices inside of a kbordermatrix?

How are presidential pardons supposed to be used?

How should I replace vector<uint8_t>::const_iterator in an API?

Sort list of array linked objects by keys and values

Can a novice safely splice in wire to lengthen 5V charging cable?

Can withdrawing asylum be illegal?

Why did all the guest students take carriages to the Yule Ball?

The following signatures were invalid: EXPKEYSIG 1397BC53640DB551

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

Finding the path in a graph from A to B then back to A with a minimum of shared edges

Can smartphones with the same camera sensor have different image quality?

University's motivation for having tenure-track positions

What was the last x86 CPU that did not have the x87 floating-point unit built in?

How is simplicity better than precision and clarity in prose?

Does the AirPods case need to be around while listening via an iOS Device?

How can I protect witches in combat who wear limited clothing?

Keeping a retro style to sci-fi spaceships?

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Do warforged have souls?

How to grep and cut numbers from a file and sum them

"... to apply for a visa" or "... and applied for a visa"?

Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?

Does Parliament hold absolute power in the UK?



How to grep and cut numbers from a file and sum them



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionGrep alias - line numbers unless it's in a pipelineIssue with real-time log inspecting piping tail, grep and cutcopy every line from a text file that contains a number greater than 5000How to unbuffer cut?Grep to filter and show only the beginning of a lineTail Grep - Print surrounding lines until pattern is matchedGrep and filter IP from text fileawk/sed/grep: Printing all lines matching a string and all lines with tabs after these linesGrep only numbers, not the alphanumeric entriesHow do I add numbers from two txt files with Bash?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I have a log file. For every line with a specific number, I want to sum the last number of those lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.



This is what I have so far:



grep "30201" logfile.txt | cut -f6 -d "|"


30201 are the lines I'm looking for.



I want to sum the last numbers 650, 1389 and 945



The logfile.txt



Jan 09 2016|09:15:17|30201|1|SL02|650
Jan 09 2016|09:15:18|43097|1|SL01|945
Jan 09 2016|09:15:19|28774|2|SB03|1389
Jan 09 2016|09:16:21|00788|1|SL02|650
Jan 09 2016|09:17:25|03361|3|SL01|945
Jan 09 2016|09:17:33|08385|1|SL02|650
Jan 09 2016|09:18:43|10234|1|SL01|945
Jan 09 2016|09:21:55|00788|1|SL02|650
Jan 09 2016|09:24:43|03361|3|SB03|1389
Jan 09 2016|09:26:01|30201|1|SB03|1389
Jan 09 2016|09:26:21|28774|2|SL02|650
Jan 09 2016|09:26:25|00788|1|SL02|650
Jan 09 2016|09:27:21|28774|2|SL02|650
Jan 09 2016|09:29:32|30201|1|SL01|945
Jan 09 2016|09:30:12|34032|1|SB03|1389
Jan 09 2016|09:30:15|08767|3|SL02|650









share|improve this question






























    3















    I have a log file. For every line with a specific number, I want to sum the last number of those lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.



    This is what I have so far:



    grep "30201" logfile.txt | cut -f6 -d "|"


    30201 are the lines I'm looking for.



    I want to sum the last numbers 650, 1389 and 945



    The logfile.txt



    Jan 09 2016|09:15:17|30201|1|SL02|650
    Jan 09 2016|09:15:18|43097|1|SL01|945
    Jan 09 2016|09:15:19|28774|2|SB03|1389
    Jan 09 2016|09:16:21|00788|1|SL02|650
    Jan 09 2016|09:17:25|03361|3|SL01|945
    Jan 09 2016|09:17:33|08385|1|SL02|650
    Jan 09 2016|09:18:43|10234|1|SL01|945
    Jan 09 2016|09:21:55|00788|1|SL02|650
    Jan 09 2016|09:24:43|03361|3|SB03|1389
    Jan 09 2016|09:26:01|30201|1|SB03|1389
    Jan 09 2016|09:26:21|28774|2|SL02|650
    Jan 09 2016|09:26:25|00788|1|SL02|650
    Jan 09 2016|09:27:21|28774|2|SL02|650
    Jan 09 2016|09:29:32|30201|1|SL01|945
    Jan 09 2016|09:30:12|34032|1|SB03|1389
    Jan 09 2016|09:30:15|08767|3|SL02|650









    share|improve this question


























      3












      3








      3


      2






      I have a log file. For every line with a specific number, I want to sum the last number of those lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.



      This is what I have so far:



      grep "30201" logfile.txt | cut -f6 -d "|"


      30201 are the lines I'm looking for.



      I want to sum the last numbers 650, 1389 and 945



      The logfile.txt



      Jan 09 2016|09:15:17|30201|1|SL02|650
      Jan 09 2016|09:15:18|43097|1|SL01|945
      Jan 09 2016|09:15:19|28774|2|SB03|1389
      Jan 09 2016|09:16:21|00788|1|SL02|650
      Jan 09 2016|09:17:25|03361|3|SL01|945
      Jan 09 2016|09:17:33|08385|1|SL02|650
      Jan 09 2016|09:18:43|10234|1|SL01|945
      Jan 09 2016|09:21:55|00788|1|SL02|650
      Jan 09 2016|09:24:43|03361|3|SB03|1389
      Jan 09 2016|09:26:01|30201|1|SB03|1389
      Jan 09 2016|09:26:21|28774|2|SL02|650
      Jan 09 2016|09:26:25|00788|1|SL02|650
      Jan 09 2016|09:27:21|28774|2|SL02|650
      Jan 09 2016|09:29:32|30201|1|SL01|945
      Jan 09 2016|09:30:12|34032|1|SB03|1389
      Jan 09 2016|09:30:15|08767|3|SL02|650









      share|improve this question
















      I have a log file. For every line with a specific number, I want to sum the last number of those lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.



      This is what I have so far:



      grep "30201" logfile.txt | cut -f6 -d "|"


      30201 are the lines I'm looking for.



      I want to sum the last numbers 650, 1389 and 945



      The logfile.txt



      Jan 09 2016|09:15:17|30201|1|SL02|650
      Jan 09 2016|09:15:18|43097|1|SL01|945
      Jan 09 2016|09:15:19|28774|2|SB03|1389
      Jan 09 2016|09:16:21|00788|1|SL02|650
      Jan 09 2016|09:17:25|03361|3|SL01|945
      Jan 09 2016|09:17:33|08385|1|SL02|650
      Jan 09 2016|09:18:43|10234|1|SL01|945
      Jan 09 2016|09:21:55|00788|1|SL02|650
      Jan 09 2016|09:24:43|03361|3|SB03|1389
      Jan 09 2016|09:26:01|30201|1|SB03|1389
      Jan 09 2016|09:26:21|28774|2|SL02|650
      Jan 09 2016|09:26:25|00788|1|SL02|650
      Jan 09 2016|09:27:21|28774|2|SL02|650
      Jan 09 2016|09:29:32|30201|1|SL01|945
      Jan 09 2016|09:30:12|34032|1|SB03|1389
      Jan 09 2016|09:30:15|08767|3|SL02|650






      text-processing grep logs cut numeric-data






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      Jeff Schaller

      45k1164147




      45k1164147










      asked 13 hours ago









      YungScholarYungScholar

      233




      233




















          4 Answers
          4






          active

          oldest

          votes


















          7














          You can take help from paste to serialize the numbers in a format suitable for bc to do the addition:



          % grep "30201" logfile.txt | cut -f6 -d "|"
          650
          1389
          945

          % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+
          650+1389+945

          % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
          2984


          If you have grep with PCRE, you can do it with grep alone using postive lookbehind:



          % grep -Po '|30201|.*|Kd+' logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
          2984



          With awk alone:



          % awk -F'|' '$3 == 30201 sum+=$NF; ENDprint sum' logfile.txt 
          2984



          • -F'|' sets the field separator as |


          • $3 == 30201 sum+=$NF adds up the last field's values if the third field is 30201


          • ENDprint sum prints the sum at the END





          share|improve this answer

























          • Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

            – YungScholar
            13 hours ago






          • 2





            Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

            – Kusalananda
            11 hours ago



















          0














          Bash solution.



          #!/bin/bash
          pa=0 ; s=0 ;
          while read a b ; do
          if [ "$a" == "$pa" ] ; then
          s=$(($s+$b)) ;
          else
          if [ "$pa" != 0 ] ; then
          echo $pa $s ;
          fi ;
          pa=$a ; s=$b ;
          fi ;
          done < <(cat j.txt | awk -F'|' 'printf("%s %sn",$3,$6)' | sort -n)
          echo $pa $s


          Init Previous A and SUM



          Cut down the input to fields 3 and 6 and sort them by number



          Loop as long as field 3 stays the same, add field 6 to the SUM



          if field 3 changes but the Previous A is not 0, output the Previous A and the SUM and reinit Previous A to a and SUM to last field 6 read.



          Output last Previous A and SUM.



          Output of the given input:



          00788 1950
          03361 2334
          08385 650
          08767 650
          10234 945
          28774 2689
          30201 2984
          34032 1389
          43097 945





          share|improve this answer























          • Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

            – icarus
            5 hours ago


















          0














          There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.



          Using bash:



          #!/bin/bash
          # get the output as a bash array and add the elements
          nums=( $(grep "|30201|" logfile.txt | cut -f6 -d "|") )
          total=0

          for i in $!nums[@]
          do
          total=$(($total+$nums[i]))
          done
          echo $total





          share|improve this answer
































            0














            One little tool I keep around I call sumcol



            #!/bin/sh
            # Icarus Sparry. Free for any use.
            C=$1:?"missing required column number"
            shift
            awk 's+=$'"$C"' END print s ' "$@"


            which adds up the whitespace delimited column you provide. Whilst I would write (as @heemayl does)



            awk -F'|' '$3 == 30201 s+=$6 END print s' logfile.txt


            for the OP's problem, he could use



            grep "30201" logfile.txt | cut -f6 -d "|" | sumcol 1


            or



            grep "30201" logfile.txt | tr "| " " _" | sumcol 6





            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%2f512250%2fhow-to-grep-and-cut-numbers-from-a-file-and-sum-them%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              You can take help from paste to serialize the numbers in a format suitable for bc to do the addition:



              % grep "30201" logfile.txt | cut -f6 -d "|"
              650
              1389
              945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+
              650+1389+945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984


              If you have grep with PCRE, you can do it with grep alone using postive lookbehind:



              % grep -Po '|30201|.*|Kd+' logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984



              With awk alone:



              % awk -F'|' '$3 == 30201 sum+=$NF; ENDprint sum' logfile.txt 
              2984



              • -F'|' sets the field separator as |


              • $3 == 30201 sum+=$NF adds up the last field's values if the third field is 30201


              • ENDprint sum prints the sum at the END





              share|improve this answer

























              • Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

                – YungScholar
                13 hours ago






              • 2





                Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

                – Kusalananda
                11 hours ago
















              7














              You can take help from paste to serialize the numbers in a format suitable for bc to do the addition:



              % grep "30201" logfile.txt | cut -f6 -d "|"
              650
              1389
              945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+
              650+1389+945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984


              If you have grep with PCRE, you can do it with grep alone using postive lookbehind:



              % grep -Po '|30201|.*|Kd+' logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984



              With awk alone:



              % awk -F'|' '$3 == 30201 sum+=$NF; ENDprint sum' logfile.txt 
              2984



              • -F'|' sets the field separator as |


              • $3 == 30201 sum+=$NF adds up the last field's values if the third field is 30201


              • ENDprint sum prints the sum at the END





              share|improve this answer

























              • Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

                – YungScholar
                13 hours ago






              • 2





                Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

                – Kusalananda
                11 hours ago














              7












              7








              7







              You can take help from paste to serialize the numbers in a format suitable for bc to do the addition:



              % grep "30201" logfile.txt | cut -f6 -d "|"
              650
              1389
              945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+
              650+1389+945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984


              If you have grep with PCRE, you can do it with grep alone using postive lookbehind:



              % grep -Po '|30201|.*|Kd+' logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984



              With awk alone:



              % awk -F'|' '$3 == 30201 sum+=$NF; ENDprint sum' logfile.txt 
              2984



              • -F'|' sets the field separator as |


              • $3 == 30201 sum+=$NF adds up the last field's values if the third field is 30201


              • ENDprint sum prints the sum at the END





              share|improve this answer















              You can take help from paste to serialize the numbers in a format suitable for bc to do the addition:



              % grep "30201" logfile.txt | cut -f6 -d "|"
              650
              1389
              945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+
              650+1389+945

              % grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984


              If you have grep with PCRE, you can do it with grep alone using postive lookbehind:



              % grep -Po '|30201|.*|Kd+' logfile.txt | cut -f6 -d "|" | paste -sd+ | bc
              2984



              With awk alone:



              % awk -F'|' '$3 == 30201 sum+=$NF; ENDprint sum' logfile.txt 
              2984



              • -F'|' sets the field separator as |


              • $3 == 30201 sum+=$NF adds up the last field's values if the third field is 30201


              • ENDprint sum prints the sum at the END






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 13 hours ago

























              answered 13 hours ago









              heemaylheemayl

              36.4k378108




              36.4k378108












              • Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

                – YungScholar
                13 hours ago






              • 2





                Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

                – Kusalananda
                11 hours ago


















              • Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

                – YungScholar
                13 hours ago






              • 2





                Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

                – Kusalananda
                11 hours ago

















              Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

              – YungScholar
              13 hours ago





              Thanks! grep "30201" logfile.txt | cut -f6 -d "|" | paste -sd+ dind't work for me, I got this errors (standard_in) 1: illegal character: ^M. But the solution with grep -Po '|30201|.*|Kd+' works. This is great!

              – YungScholar
              13 hours ago




              2




              2





              Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

              – Kusalananda
              11 hours ago






              Note that the grep solution does not care in what column the number is found, or whether the number is just a substring of a longer number. The awk solution is safer in this respect. The grep solution could be improved by first cutting and the matching the number at the start of the line (followed by |) with proper anchoring.

              – Kusalananda
              11 hours ago














              0














              Bash solution.



              #!/bin/bash
              pa=0 ; s=0 ;
              while read a b ; do
              if [ "$a" == "$pa" ] ; then
              s=$(($s+$b)) ;
              else
              if [ "$pa" != 0 ] ; then
              echo $pa $s ;
              fi ;
              pa=$a ; s=$b ;
              fi ;
              done < <(cat j.txt | awk -F'|' 'printf("%s %sn",$3,$6)' | sort -n)
              echo $pa $s


              Init Previous A and SUM



              Cut down the input to fields 3 and 6 and sort them by number



              Loop as long as field 3 stays the same, add field 6 to the SUM



              if field 3 changes but the Previous A is not 0, output the Previous A and the SUM and reinit Previous A to a and SUM to last field 6 read.



              Output last Previous A and SUM.



              Output of the given input:



              00788 1950
              03361 2334
              08385 650
              08767 650
              10234 945
              28774 2689
              30201 2984
              34032 1389
              43097 945





              share|improve this answer























              • Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

                – icarus
                5 hours ago















              0














              Bash solution.



              #!/bin/bash
              pa=0 ; s=0 ;
              while read a b ; do
              if [ "$a" == "$pa" ] ; then
              s=$(($s+$b)) ;
              else
              if [ "$pa" != 0 ] ; then
              echo $pa $s ;
              fi ;
              pa=$a ; s=$b ;
              fi ;
              done < <(cat j.txt | awk -F'|' 'printf("%s %sn",$3,$6)' | sort -n)
              echo $pa $s


              Init Previous A and SUM



              Cut down the input to fields 3 and 6 and sort them by number



              Loop as long as field 3 stays the same, add field 6 to the SUM



              if field 3 changes but the Previous A is not 0, output the Previous A and the SUM and reinit Previous A to a and SUM to last field 6 read.



              Output last Previous A and SUM.



              Output of the given input:



              00788 1950
              03361 2334
              08385 650
              08767 650
              10234 945
              28774 2689
              30201 2984
              34032 1389
              43097 945





              share|improve this answer























              • Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

                – icarus
                5 hours ago













              0












              0








              0







              Bash solution.



              #!/bin/bash
              pa=0 ; s=0 ;
              while read a b ; do
              if [ "$a" == "$pa" ] ; then
              s=$(($s+$b)) ;
              else
              if [ "$pa" != 0 ] ; then
              echo $pa $s ;
              fi ;
              pa=$a ; s=$b ;
              fi ;
              done < <(cat j.txt | awk -F'|' 'printf("%s %sn",$3,$6)' | sort -n)
              echo $pa $s


              Init Previous A and SUM



              Cut down the input to fields 3 and 6 and sort them by number



              Loop as long as field 3 stays the same, add field 6 to the SUM



              if field 3 changes but the Previous A is not 0, output the Previous A and the SUM and reinit Previous A to a and SUM to last field 6 read.



              Output last Previous A and SUM.



              Output of the given input:



              00788 1950
              03361 2334
              08385 650
              08767 650
              10234 945
              28774 2689
              30201 2984
              34032 1389
              43097 945





              share|improve this answer













              Bash solution.



              #!/bin/bash
              pa=0 ; s=0 ;
              while read a b ; do
              if [ "$a" == "$pa" ] ; then
              s=$(($s+$b)) ;
              else
              if [ "$pa" != 0 ] ; then
              echo $pa $s ;
              fi ;
              pa=$a ; s=$b ;
              fi ;
              done < <(cat j.txt | awk -F'|' 'printf("%s %sn",$3,$6)' | sort -n)
              echo $pa $s


              Init Previous A and SUM



              Cut down the input to fields 3 and 6 and sort them by number



              Loop as long as field 3 stays the same, add field 6 to the SUM



              if field 3 changes but the Previous A is not 0, output the Previous A and the SUM and reinit Previous A to a and SUM to last field 6 read.



              Output last Previous A and SUM.



              Output of the given input:



              00788 1950
              03361 2334
              08385 650
              08767 650
              10234 945
              28774 2689
              30201 2984
              34032 1389
              43097 945






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 12 hours ago









              JdeHaanJdeHaan

              359214




              359214












              • Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

                – icarus
                5 hours ago

















              • Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

                – icarus
                5 hours ago
















              Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

              – icarus
              5 hours ago





              Given you are using awk anyway to select the third and sixth columns, you should go the extra steps and sum things inside awk. This would give you something like awk -F'|' 's[$3]+=$6END for (i in s) print i, s[i] ' | sort - GNU awk has a builtin asort which could also be used rather than an external sort.

              – icarus
              5 hours ago











              0














              There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.



              Using bash:



              #!/bin/bash
              # get the output as a bash array and add the elements
              nums=( $(grep "|30201|" logfile.txt | cut -f6 -d "|") )
              total=0

              for i in $!nums[@]
              do
              total=$(($total+$nums[i]))
              done
              echo $total





              share|improve this answer





























                0














                There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.



                Using bash:



                #!/bin/bash
                # get the output as a bash array and add the elements
                nums=( $(grep "|30201|" logfile.txt | cut -f6 -d "|") )
                total=0

                for i in $!nums[@]
                do
                total=$(($total+$nums[i]))
                done
                echo $total





                share|improve this answer



























                  0












                  0








                  0







                  There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.



                  Using bash:



                  #!/bin/bash
                  # get the output as a bash array and add the elements
                  nums=( $(grep "|30201|" logfile.txt | cut -f6 -d "|") )
                  total=0

                  for i in $!nums[@]
                  do
                  total=$(($total+$nums[i]))
                  done
                  echo $total





                  share|improve this answer















                  There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.



                  Using bash:



                  #!/bin/bash
                  # get the output as a bash array and add the elements
                  nums=( $(grep "|30201|" logfile.txt | cut -f6 -d "|") )
                  total=0

                  for i in $!nums[@]
                  do
                  total=$(($total+$nums[i]))
                  done
                  echo $total






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 8 hours ago

























                  answered 8 hours ago









                  WastrelWastrel

                  11




                  11





















                      0














                      One little tool I keep around I call sumcol



                      #!/bin/sh
                      # Icarus Sparry. Free for any use.
                      C=$1:?"missing required column number"
                      shift
                      awk 's+=$'"$C"' END print s ' "$@"


                      which adds up the whitespace delimited column you provide. Whilst I would write (as @heemayl does)



                      awk -F'|' '$3 == 30201 s+=$6 END print s' logfile.txt


                      for the OP's problem, he could use



                      grep "30201" logfile.txt | cut -f6 -d "|" | sumcol 1


                      or



                      grep "30201" logfile.txt | tr "| " " _" | sumcol 6





                      share|improve this answer



























                        0














                        One little tool I keep around I call sumcol



                        #!/bin/sh
                        # Icarus Sparry. Free for any use.
                        C=$1:?"missing required column number"
                        shift
                        awk 's+=$'"$C"' END print s ' "$@"


                        which adds up the whitespace delimited column you provide. Whilst I would write (as @heemayl does)



                        awk -F'|' '$3 == 30201 s+=$6 END print s' logfile.txt


                        for the OP's problem, he could use



                        grep "30201" logfile.txt | cut -f6 -d "|" | sumcol 1


                        or



                        grep "30201" logfile.txt | tr "| " " _" | sumcol 6





                        share|improve this answer

























                          0












                          0








                          0







                          One little tool I keep around I call sumcol



                          #!/bin/sh
                          # Icarus Sparry. Free for any use.
                          C=$1:?"missing required column number"
                          shift
                          awk 's+=$'"$C"' END print s ' "$@"


                          which adds up the whitespace delimited column you provide. Whilst I would write (as @heemayl does)



                          awk -F'|' '$3 == 30201 s+=$6 END print s' logfile.txt


                          for the OP's problem, he could use



                          grep "30201" logfile.txt | cut -f6 -d "|" | sumcol 1


                          or



                          grep "30201" logfile.txt | tr "| " " _" | sumcol 6





                          share|improve this answer













                          One little tool I keep around I call sumcol



                          #!/bin/sh
                          # Icarus Sparry. Free for any use.
                          C=$1:?"missing required column number"
                          shift
                          awk 's+=$'"$C"' END print s ' "$@"


                          which adds up the whitespace delimited column you provide. Whilst I would write (as @heemayl does)



                          awk -F'|' '$3 == 30201 s+=$6 END print s' logfile.txt


                          for the OP's problem, he could use



                          grep "30201" logfile.txt | cut -f6 -d "|" | sumcol 1


                          or



                          grep "30201" logfile.txt | tr "| " " _" | sumcol 6






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 5 hours ago









                          icarusicarus

                          6,21611231




                          6,21611231



























                              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%2f512250%2fhow-to-grep-and-cut-numbers-from-a-file-and-sum-them%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

                              名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                              Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

                              Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar