0

I am trying to get the sum of my output in bash shell using only awk. One of the problems I am getting is that I only need to use awk in this.

This is the code I am using for getting the output: awk '{print substr($7, 9, 4)}' emp.txt

This is the output I am getting: (output omitted) 7606 6498 7947 4044 1657 3872 4834 8463 9280 2789 9104

this is how I am trying to do the sum of the numbers: awk '(s = s + substr($7, 9, 4)) {print s}' emp.txt

The problem is that it is not giving me the right output (which should be 9942686) but instead giving me the series sum (as shown below). (output omitted) 9890696 9898643 9902687 9904344 9908216 9913050 9921513 9930793 9933582 9942686

Am I using the code the wrong way? Or is there any other method of doing it with awk and I am doing it the wrong way?

Here is the sample file I am working on:

Brynlee Watkins F 55 Married 2016 778-555-6498 62861 Malcolm Curry M 24 Married 2016 604-555-7947 54647 Aylin Blake F 45 Married 2015 236-555-4044 80817 Mckinley Hodges F 50 Married 2015 604-555-1657 46316 Rylan Dorsey F 51 Married 2017 778-555-3872 77160 Taylor Clarke M 23 Married 2015 604-555-4834 46624 Vivaan Hooper M 26 Married 2016 778-555-8463 80010 Gibson Rowland M 42 Married 2017 236-555-9280 59874 Alyson Mahoney F 51 Single 2017 778-555-2789 71394 Catalina Frazier F 53 Married 2016 604-555-9104 79364

EDIT: I want to get the sum of the numbers that are repeating in the output. Let's say the repeating numbers are 4826 and 0028 in the output and both of them repeated 2 times. I only want the sum of these numbers (each repetition must be counted as the individual. hence these are counted as 4). So the desired output for these 4 numbers shall be 9708

Will Duffy M 33 Single 2017 236-555-4826 47394 Nolan Reed M 27 Single 2015 604-555-0028 46622 Anya Horn F 54 Married 2017 236-555-4826 73270 Cynthia Davenport F 29 Married 2015 778-555-0028 59687 Oscar Medina M 43 Married 2016 778-555-7864 73688 Angelina Herrera F 37 Married 2017 604-555-7910 82061 Peyton Reyes F 35 Married 2017 236-555-8046 51920

2
  • Is it really (s = s + substr($7, 9, 4)) in your code? This is really a pattern that is evaluated per each line (so luckily it does the calculation you need). The outer parentheses should probably be braces instead. Commented Feb 15, 2018 at 3:58
  • So it is not the way how is it done? is there a better way somehow using awk? Commented Feb 15, 2018 at 4:01

2 Answers 2

1
END { print s }

Since you only need the total sum printed once, do it under the END pattern.

awk '{s = s + substr($7, 9, 4)} END {print s}' emp.txt
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried it before using END pattern, and it showed me a syntax error near the starting of END
Well, if it was that parenthesized expression before END, then it might result in error for it was two patterns in a row and awk usually does not permit such constructs (implicit action is only invoked when the whole script is but a single pattern). Change parentheses into braces and put END back and then see what happens.
Thanks very much sir! That worked great for me! Just curious if I can put a condition as if I only wanted the sum of those numbers which are repeating in this output.
@GaganpreetSinghMaan, please post example of it once in your post with CODE TAGS.
@bipli please see the edited question and the sample file with repeated last 4 digits
0

Could you please try following awk and let me know if this helps you. It will look always look for last digits after -:

awk -F' |-' '{sum+=$(NF-1)} END{print sum}'  Input_file

EDIT:

awk -F' |-' '
{
  ++a[$(NF-1)];
  b[$(NF-1)]=b[$(NF-1)]?b[$(NF-1)]+$(NF-1):$(NF-1)
}
END{
  for(i in a){
    if(a[i]>1){
     print i,b[i]}
}}
'   Input_file

Output will be as follows:

4826 9652
0028 56

5 Comments

That works great! Just a few addition: I onlyu wanted the sum of numbers that are repeating in the end... so is there any command I can use in addition to get that>
@GaganpreetSinghMaan, could you please do let me know which repeating numbers you are referring here?
please see the edited question and the sample file with repeated last 4 digits
@GaganpreetSinghMaan, please check my EDIT code now and let me know if this helps you.
@GaganpreetSinghMaan, Veere, could you please do lemme know if this helped you?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.