Breaking the sum of a column when specific pattern is found

Hi

Am trying to sum up a file

# cat /Out
maths                   2            0.0          
english                2091            0.2          
history               -1            0.0           
physics                18            0.0
--------------------------------------
maths                   2            0.0          
english                2091            0.2          
history               -1            0.0           
physics                18            0.0
-------------------------------------
maths                   2            0.0          
english                2091            0.2          
history               -1            0.0           
physics                18            0.0

Am trying to sum up only the second column of the file
I need to sum up like, When "-------------" is met the values that had sum up so far should be stored in another variable and then a new sum from zero has to be proceeded until again "-------------------" is met and store the value in another new variable

I reached till

we=`awk '{print $2}'  /Out |grep '[0-9]'|awk '{sum+=$1} END {print sum}'`

But this gives the sum of entire column..
Please help..

Can you try this,

awk '{print $2}' /Out | grep '[0-9]' | awk '{RS="----"} {sum+=$1} END {print sum}'

Thanks
Latika

So, do you need the result in some kind of shell script? Then you would probably be better off with a shell loop and adding an resetting the totals inside the loop..

while read subject col2 col3
do
  case $subject in 
    --*) printf "%d\n" "$total"
         # Here some further processing with $total
         # ...
         # reset counter
         total=0 ;;
      *) total=$(( total + col2 ))
  esac
done < file
printf "%d\n" "$total"

I tried the following code

# awk '{print $2}' /out | grep '[0-9]' | awk '{RS="----"} {sum+=$1} END {print sum}'
2094

This prints the last sum value between "---------" and "-------------" i need to print all sum values between "---------" and "-------------"

For eg.,
first SUM 10
second SUM 20
like wise between "---------" and "-------------" i need the total value

Why store in variables?
This one prints

awk '{s+=$2} /----/ {print s; s=0}'

Is this the area you like to sum (in red)?

maths                   2            0.0          
english                2091            0.2          
history               -1            0.0           
physics                18            0.0
--------------------------------------
maths                   2            0.0          
english                2091            0.2          
history               -1            0.0           
physics                18            0.0
-------------------------------------
maths                   2            0.0          
english                2091            0.2          
history               -1            0.0           
physics                18            0.0
awk '/---/{if (f==0) f=1; else f=0} f{s+=$2} END {print s}'
2110
1 Like

Hi Jotne,

Yes, You are right..
I need to sum up the values marked in RED..
But what I need is like

Code:
  maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0 -------------------------------------- maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0 ------------------------------------- maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0
-----------------------------------------
maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0
------------------------------------------

Here I need to find 4 sums between "---------" and "-------------"

But the code You suggested works only for one set of values that are marked in RED

I have tried a set of code like

cat New | awk '{print $2}' > trick
lineCnt=`cat trick | wc -l`
echo "The total number of lines is $lineCnt"
line=1
while [[ $line -le $lineCnt ]] && read LINE; do
    if [[ $LINE = "-----" ]]; then
        line=$((line+1))
        echo "The previous total is $total"
        total=0
    else
    #total=$LINE
    total=$((total+LINE))
    fi
done < trick

But I get the following error

./DataCompare.sh
The total number of lines is       19
The previous total is
./DataCompare.sh[14]: ------: 0403-053 Expression is not complete; more tokens expected.

Please correct if am wrong anywhere..

Code:
  maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0 -------------------------------------- maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0 ------------------------------------- maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0
-----------------------------------------
maths                   2            0.0           english                2091            0.2           history               -1            0.0            physics                18            0.0
------------------------------------------

This is not clear at all, just a long line.
Where did the sum go?

Hi Jotne
Here is the correct file

maths                   2            0.0
english                2091            0.2
history               -1            0.0
physics                18            0.0
--------------------------------------
maths                   2            0.0
english                2091            0.2
history               -1            0.0
physics                18            0.0
--------------------------------------
maths                   2            0.0
english                2091            0.2
history               -1            0.0
physics                18            0.0
--------------------------------------
maths                   2            0.0
english                2091            0.2
history               -1            0.0
physics                18            0.0
--------------------------------------
maths                   2            0.0
english                2091            0.2
history               -1            0.0
physics                18            0.0
--------------------------------------

Here i need 5 sum up values between "----" and "-----"

$LINE is not a number but an entire line, so you cannot add it like that..
Have a look at my suggestion in post #3