[Solved] Find Specific records from file and add totals into variables

Hi Eveyone,

I am working on one shell script to find the specific records from data file and add the totals into variables and print them. you can find the sample data file below for more clarification.

Sample Data File:

PXSTYL00__20090803USA
CHCART00__20090803IND
SSTRAN00__20090803USA
Records Sent: 100 Sum1: 10 20090803 1234567890 1234567890 123456789 123456789 123456789
Records Sent: 200 Sum1: 20 20090803 1234567890 1234567890 123456789 123456789 123456789
Records Sent: 300 Sum1: 30 20090803 1234567890 1234567890 123456789 123456789 123456789

In the above sample data file, I want to read all Records with word Records Sent and then get the totals and sums for all records together.

Out Put Should be like this

Total Records Sent: 100+200+300 = 600
Total Sum : 10+20+30 = 60

I tried the following code:

total = 0
sum = 0
grep "Records Sent:" filename > filename.tmp
cat filename.tmp |\
while read line
do
tot = `$line | cut -f3 -d " "`
su = `$line | cut -f5 -d " "`
total ='expr $total + $total`
sum =`expr $sum +su`
done
echo $total
echo $sum

I am having some issues while running this code. Those issues are
when i am using while read line so it will try to goby line by line but basically it should go by record by record because the Record which has Records Sent: will be 2 lines.
I had another error saying cannot create. I have no idea why this error is coming.
Can anybody write above script in different way.Please advise me on this

Best Regards

total=0
summ=0
grep "Records Sent:" filename | while read line
do
tot=`$line | cut -f3 -d " "`
suu=`$line | cut -f5 -d " "`
total=`expr $total + $tot`
summ=`expr $summ + suu`
done
echo "Total = $total"
echo "Sum = $summ"
  1. Removed space before/after all "="
  2. Replaced reserved word "su" with "suu" and "sum" with "summ"
  3. Typo error in total ='expr.....Should be total=`expr
  4. Typo error. Should be $total + $tot`
nawk '$1=="Records" && $2=="Sent:" && $4="Sum1:" {sent+=$3;sum+=$5} END{print sent" "sum}'

I tried the following code but i am getting below errors

Code:
total=0
summ=0
grep "Records Sent:" filename | while read line
do
tot=`$line | cut -f3 -d " "`
suu=`$line | cut -f5 -d " "`
total=`expr $total + $tot`
summ=`expr $summ + suu`
done
echo "Total = $total"
echo "Sum = $summ"

Errors
expr: non-numeric argument
expr: syntax error
expr: syntax error

Base on your data sample

# awk '/Records Sent/{a[$1FS$2]+=$3;b[$4]+=$5}END{for(i in a)print v,i,a;for(i in b)print v,i,b}' v="Total" file
Total Records Sent: 600
Total Sum1: 60

I don't want to rewrite everything because I have lot other validations included in the same script. My below code is working for total but not for sum because I have some decimals in my sum.

Code:
total=0
summ=0
grep "Records Sent:" filename | while read line
do
tot=`$line | cut -f3 -d " "`
suu=`$line | cut -f5 -d " "`
total=`expr $total + $tot`
summ=`expr $summ + $suu`
done
echo "Total = $total"
echo "Sum = $summ"

The above code is working to get the total but not for sum because I have some decimals in sum. please let me know how to get rid of those errors

Errors:

expr: non-numeric argument
expr: syntax error
expr: syntax error

Hi.

Try using bc instead of eval

summ=$(echo "$summ + $suu" | bc)

It's working

Thanks a Lot