# 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..
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"
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.